Keywords Identifiers: Tokens in C
Keywords Identifiers: Tokens in C
These are reserved words of the C language. For example int, float, if, else, for, while etc.
Identifiers
An Identifier is a sequence of letters and digits, but must start with a letter. Underscore ( _ ) is treated as a letter. Identifiers are case sensitive. Identifiers are used to name variables, functions etc. Valid: Root, _getchar, __sin, x1, x2, x3, x_1, If Invalid: 324, short, price$, My Name
Constants
Constants like 13, a, 1.3e-5 etc.
Operators
Arithmetic operators like +, -, *, / ,% etc. Logical operators like ||, &&, ! etc. and so on.
White Spaces
Spaces, new lines, tabs, comments ( A sequence of characters enclosed in /* and */ ) etc. These are used to separate the adjacent identifiers, kewords and constants.
Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign bit is 0, the number is treated as positive. Bit pattern 01001011 = 75 (decimal). The largest positive number is 01111111 = 27 1 = 127. Negative numbers are stored as twos complement or as ones complement. -75 = 10110100 (ones complement). -75 = 10110101 (twos complement).
/* Local */
printf( Enter the radius ); scanf(%f , &rad); if ( rad > 0.0 ) { float area = pi * rad * rad; float peri = 2 * pi * rad; printf( Area = %f\n , area ); printf( Peri = %f\n , peri ); } else printf( Negative radius\n); printf( Area = %f\n , area ); }
10
/* Local */
printf( Enter the radius ); scanf(%f , &rad); if ( rad > 0.0 ) { float area = pi * rad * rad; float peri = 2 * pi * rad; printf( Area = %f\n , area ); printf( Peri = %f\n , peri ); } else printf( Negative radius\n); printf( Area = %f\n , area ); }
11
12
13
Type Conversions
The operands of a binary operator must have a the same type and the result is also of the same type. Integer division: c = (9 / 5)*(f - 32) The operands of the division are both int and hence the result also would be int. For correct results, one may write c = (9.0 / 5.0)*(f - 32) In case the two operands of a binary operator are different, but compatible, then they are converted to the same type by the compiler. The mechanism (set of rules) is called Automatic Type Casting. c = (9.0 / 5)*(f - 32) It is possible to force a conversion of an operand. This is called Explicit Type casting. c = ((float) 9 / 5)*(f - 32)
14
15
16
17
18
19
20
21
22
0.d1d2 d p Be
23
24