@@ -11,6 +11,7 @@ contributors:
11
11
- ["Joshua Li", "https://github.com/JoshuaRLi"]
12
12
- ["Dragos B. Chirila", "https://github.com/dchirila"]
13
13
- ["Heitor P. de Bittencourt", "https://github.com/heitorPB/"]
14
+ - ["Mykolas Bamberg", "https://github.com/MykBamberg"]
14
15
---
15
16
16
17
Ah, C. Still ** the** language of modern high-performance computing.
@@ -33,6 +34,9 @@ Multi-line comments don't nest /* Be careful */ // comment ends on this line...
33
34
// Constants: #define <keyword>
34
35
// Constants are written in all-caps out of convention, not requirement
35
36
#define DAYS_IN_YEAR 365
37
+ // Since C23 it is also possible to define constants using the constexpr keyword
38
+ // which gives you the ability to easily specify the type of the constant
39
+ constexpr int DAYS_IN_YEAR = 365 ;
36
40
37
41
// Enumeration constants are also ways to declare constants.
38
42
// All statements must end with a semicolon
@@ -119,8 +123,8 @@ int main (int argc, char** argv)
119
123
char x_char = 0;
120
124
char y_char = 'y'; // Char literals are quoted with ''
121
125
122
- // longs are often 4 to 8 bytes; long longs are guaranteed to be at least
123
- // 8 bytes
126
+ // longs are often 4 to 8 bytes; long longs (added in C99) are guaranteed to
127
+ // be at least 8 bytes
124
128
long x_long = 0;
125
129
long long x_long_long = 0;
126
130
@@ -191,7 +195,7 @@ int main (int argc, char** argv)
191
195
// time constant:
192
196
printf("Enter the array size: "); // ask the user for an array size
193
197
int array_size;
194
- fscanf(stdin, "%d", &array_size);
198
+ scanf( "%d", &array_size);
195
199
int var_length_array[ array_size] ; // declare the VLA
196
200
printf("sizeof array = %zu\n", sizeof var_length_array);
197
201
@@ -265,11 +269,10 @@ int main (int argc, char** argv)
265
269
(-11) % 3; // => -2, as one would expect
266
270
11 % (-3); // => 2 and not -2, and it's quite counter intuitive
267
271
268
- // Comparison operators are probably familiar, but
269
- // there is no Boolean type in C. We use ints instead.
270
- // (C99 introduced the _ Bool type provided in stdbool.h)
271
- // 0 is false, anything else is true. (The comparison
272
- // operators always yield 0 or 1.)
272
+ // Comparison operators are probably familiar. However, instead of the
273
+ // result being of a Boolean type (introduced as _ Bool in the C99 standard
274
+ // and called bool since C23) it is represented by an int, 0 is the value
275
+ // of a false statement and 1 is the value of a true statement.
273
276
3 == 2; // => 0 (false)
274
277
3 != 2; // => 1 (true)
275
278
3 > 2; // => 1
@@ -358,11 +361,9 @@ int main (int argc, char** argv)
358
361
// ***** NOTES***** :
359
362
// Loops and Functions MUST have a body. If no body is needed:
360
363
int i;
361
- for (i = 0; i <= 5; i++) {
362
- ; // use semicolon to act as the body (null statement)
363
- }
364
+ for (i = 0; i <= 5; i++) {} // use an empty compound statement
364
365
// Or
365
- for (i = 0; i <= 5; i++);
366
+ for (i = 0; i <= 5; i++); // use semicolon to act as the body (null statement)
366
367
367
368
// branching with multiple choices: switch()
368
369
switch (a) {
@@ -387,15 +388,15 @@ int main (int argc, char** argv)
387
388
/*
388
389
Using "goto" in C
389
390
* /
390
- typedef enum { false, true } bool ;
391
+ typedef enum { FALSE, TRUE } boolean ;
391
392
// for C don't have bool as data type before C99 :(
392
- bool disaster = false ;
393
+ boolean disaster = FALSE ;
393
394
int i, j;
394
395
for(i=0; i<100; ++i)
395
396
for(j=0; j<100; ++j)
396
397
{
397
398
if((i + j) >= 150)
398
- disaster = true ;
399
+ disaster = TRUE ;
399
400
if(disaster)
400
401
goto error; // exit both for loops
401
402
}
@@ -418,9 +419,11 @@ int main (int argc, char** argv)
418
419
// Every value in C has a type, but you can cast one value into another type
419
420
// if you want (with some constraints).
420
421
421
- int x_hex = 0x01; // You can assign vars with hex literals
422
- // binary is not in the standard, but allowed by some
423
- // compilers (x_bin = 0b0010010110)
422
+ // You can assign variables with hex literals.
423
+ int x_hex = 0x01;
424
+ // Binary literals were introduced in the C23 standard, however most
425
+ // compilers already supported them beforehand
426
+ int x_bin = 0b0010010110;
424
427
425
428
// Casting between types will attempt to preserve their numeric values
426
429
printf("%d\n", x_hex); // => Prints 1
@@ -813,6 +816,7 @@ typedef void (*my_fnp_type)(char *);
813
816
// it as an argument to ` printf ` .
814
817
"%x"; // hexadecimal
815
818
"%o"; // octal
819
+ "%b"; // binary (only supported since C23)
816
820
"%%"; // prints %
817
821
* /
818
822
0 commit comments