CHAPTERS 2-3 ============ Tokens: keywords, identifiers, constants, operators and punctuation. Keywords ======== auto do goto signed unsigned break double if sizeof void case else int static volatile char enum long struct while const extern register switch continue float return typedef default for short union Compilation process: Preprocessing -> lexical analysis -> syntax analysis -> semantic analysis ============================================================= fundamental data types: long form --------------------------------- char signed char unsigned char signed short int signed int signed long int usigned short int unsigned int unsigned long int float double long double fundamental data types ---------------------- char signed char unsigned char short int long usigned short unsigned unsigned long float double long double fundamental data types grouped by funcionality ---------------------------------------------- integral types: char signed char unsigned char short int long usigned short unsigned unsigned long floating types: float double long double arithmetic types: integral types + floating types ============================================================================ some character constants and their integer values ------------------------------------------------- character: 'a' 'b' 'c' ... 'z' integer value: 97 98 99 ... 112 character: 'A' 'B' 'C' ... 'Z' integer value: 65 66 67 ... 90 character: '0' '1' '2' ... '9' integer value: 48 49 50 ... 57 character: '&' '*' '+' integer value: 38 42 43 name of character written in C integer value ------------------------------------------------------- alert \a 7 backslash \\ 92 horizontal tab \t 9 newline \n 10 null character \0 0 ============================================================================ char c = 'a'; printf("%c", c); /* a is printed */ printf("%d", c); /* 97 is printed */ printf("%c%c%c", c, c+1, c+2); /* abc is printed */ ------------------------------------- char c; int i; for (i='a'; i<='z'; ++i) printf("%c", i); /* abc...z is printed */ for (c=65; c<=90; ++c) printf("%c", c); /* ABC...Z is printed */ for (c='0'; c<='9'; ++c) printf("%d", c); /* 48 49 50 ... 57 is printed */ ============================================================================ (uint-max.c) #include #include int main(void) { int i; unsigned u = UINT_MAX; /* typically 4294967295 */ for (i = 0; i < 5; ++i) printf("%u + %d = %u\n", u, i, u + i); for (i = 0; i < 5; ++i) printf("%u * %d = %u\n", u, i, u * i); return 0; } ----------------------------------------------------------------- 4294967295 + 0 = 4294967295 4294967295 + 1 = 0 4294967295 + 2 = 1 4294967295 + 3 = 2 4294967295 + 4 = 3 4294967295 * 0 = 0 4294967295 * 1 = 4294967295 4294967295 * 2 = 4294967294 4294967295 * 3 = 4294967293 4294967295 * 4 = 4294967292 ========================================================================= /* Decimal, hexadecimal, octal conversions. */ #include int main(void) { printf("%d %x %o\n", 19, 19, 19); /* 19 13 23 */ printf("%d %x %o\n", 0x1c, 0x1c, 0x1c); /* 28 1c 34 */ printf("%d %x %o\n", 017, 017, 017); /* 15 f 17 */ printf("%d\n", 11 + 0x11 + 011); /* 37 */ printf("%x\n", 2097151); /* 1fffff */ printf("%d\n", 0x1FfFFf); /* 2097151 */ return 0; } ============================================================================= (size-of-type.c) /* Compute the size of some fundamental types. */ #include int main(void) { printf("The size of some fundamental types is computed.\n\n"); printf(" char:%3d byte \n", sizeof(char)); printf(" short:%3d bytes\n", sizeof(short)); printf(" int:%3d bytes\n", sizeof(int)); printf(" long:%3d bytes\n", sizeof(long)); printf(" unsigned:%3d bytes\n", sizeof(unsigned)); printf(" float:%3d bytes\n", sizeof(float)); printf(" double:%3d bytes\n", sizeof(double)); printf("long double:%3d bytes\n", sizeof(long double)); return 0; } ------------------------------------------------------------ The size of some fundamental types is computed. char: 1 byte short: 2 bytes int: 4 bytes long: 4 bytes unsigned: 4 bytes float: 4 bytes double: 8 bytes long double: 12 bytes ----------------------------------------------------------------- sizeof(char) = 1 sizeof(short) <= sizeof(int) <= sizeof(long) sizeof(signed) = sizeof(unsigned) = sizeof(int) sizeof(float) <= sizeof(double) <= sizeof(long double) ==================================================================== (sqr-pow.c) #include #include int main(void) { double x; printf("\n%s\n%s\n%s\n\n", "The square root of x and x raised", "to the x power will be computed.", "---"); while (1) { /* do it forever */ printf("Input x: "); scanf("%lf", &x); if (x >= 0.0) printf("\n%15s%22.15e\n%15s%22.15e\n%15s%22.15e\n\n", "x = ", x, "sqrt(x) = ", sqrt(x), "pow(x, x) = ", pow(x, x)); else printf("\nSorry, your number must be nonnegative.\n\n"); } return 0; } ----------------------------------------------------------------- The square root of x and x raised to the x power will be computed. --- Input x: 2 x = 2.000000000000000e+000 sqrt(x) = 1.414213562373095e+000 pow(x, x) = 4.000000000000000e+000 Input x: ===================================================================== The usual arithmetic conversion ------------------------------- If either operand is of type {long double}, the other operand is converted to {long double}. Otherwise, if either operand is of type {double}, the other operand is converted to {double}. Otherwise, if either operand is of type {float}, the other operand is converted to {float}. Otherwise, the "integral promotions" are performed on both operands, and the following rules are applied: If either operand is of type {unsigned long}, the other operand is converted to {unsigned long}. Otherwise, if one operand has type {long} and the other operand has type {unsigned} then one of two possibilities occurs: If a long can represent all the values of an {unsigned}, then the operand of type {unsigned} is converted to {long}. If a long cannot represent all the values of an {unsigned}, then both operands are converted to {unsigned long}. Otherwise, if either operand is of type {long}, the other operand is converted to {long}. Otherwise, if either operand is of type {unsigned}, the other operand is converted to {unsigned}. Otherwise, both operands have type {int}. ---------------------------- char c; short s; int i; long l; unsigned u; unsigned long ul; float f; double d; long double ld; expression type ---------------------------- c - s / i int u * 2.0 - i double c + 3 int c + 5.0 double d + s double 2 * i / l long expression type ---------------------------- u * 7 - i unsigned f * 7 - i float 7 * s * ul unsigned long ld + c long double u - ul unsigned long u - l *system dependent*