BBS Yapısal Programlama (Structured Programming)1 Lexical Elements Kinds of tokens in C : Keywords Identifiers Constants Operators Punctuators
BBS Yapısal Programlama (Structured Programming)2 Keywords autobreakcasecharconst continuedefaultdodoubleelse enumexternfloatforgoto ifintlongregisterreturn shortsignedsizeofstaticstruct switchtypedefunionunsignedvoid volatilewhile
BBS Yapısal Programlama (Structured Programming)3 Identifiers Sequence of letters, digits, and the special character _. A letter or underscore must be the 1 st character of an identifier. –For this class, don’t use identifiers that begin with an underscore. C is case-sensitive: –Apple and apple are two different identifiers.
BBS Yapısal Programlama (Structured Programming)4 Identifiers (cont.) Valid variable names: n x _id num1 a_long_identifier Invalid variable names: var.1 num!2 not#this 126East +more
BBS Yapısal Programlama (Structured Programming)5 Declarations All variables must be declared before use. A declaration specifies a type, and contains a list of one or more variables of that type. int lower, upper, step; int c, line; A variable may be initialized in its declaration. int i = 0; Variables for which there is no explicit initialization have undefined (garbage) values.
BBS Yapısal Programlama (Structured Programming)6 Constants C manipulates various kinds of values. integer constants: 0, 37, 2001 floating constants: 0.8, , 1.0 character constants: ‘a’, ‘5’, ‘+’ string constants: “a”, “Monday”
BBS Yapısal Programlama (Structured Programming)7 Arithmetic Operators The binary arithmetic operators are +, -, *, /, and the modulus operator %. x % y produces the remainder when x is divided by y. 11 % 5 = 1 20 % 3 = 2 Arithmetic operators associate left to right.
BBS Yapısal Programlama (Structured Programming)8 Precedence and Associativity of Operators Operator Precedence: x = * 3; (What is the value of x?) x = 1 + (2*3); x is 7? or x= (1+2) * 3; x is 9? Associativity: (left to right) – / 5 * 2
BBS Yapısal Programlama (Structured Programming)9 Assignment Operator The expression can simply be a constant or a variable: int x, y; x = 5; y = x; x = 6; The expression can be an arithmetic expression: x = y + 1; y = x * 2; variable = expression
BBS Yapısal Programlama (Structured Programming)10 Assignment Compatibility int x = 3; double y =12.8; x = y;truncates y! y = x;it is okay. x = ;truncates the result! y = ;it is okay. x = 10/4;x is 2 y = 10/4;y is 2.0 y = 10/4.0;y is 2.5
BBS Yapısal Programlama (Structured Programming)11 Increment and Decrement Operators The increment operator ++ adds 1 to its operand. ++i; equiv. i = i + 1; i++; equiv.i = i + 1; The decrement operator -- subtracts 1 from its operand. --i; equiv. i = i - 1; i--; equiv. i = i - 1;
BBS Yapısal Programlama (Structured Programming)12 Increment and Decrement Operators Suppose n = 5. n++; /* sets n to 6 */ n = n + 1; ++n; /* sets n to 6 */ n = n + 1;
BBS Yapısal Programlama (Structured Programming)13 Increment and Decrement Operators (cont.) When ++a is used in an expression, the value of a is incremented before the expression is evaluated. When a++ is used, the expression is evaluated with the current value of a and then a is incremented. Similarly, with --a and a--.
BBS Yapısal Programlama (Structured Programming)14 Increment and Decrement Operators (cont.) Suppose n = 5. x = n++; /* sets x to 5 and n to 6 */ 1. x = n; 2. n = n + 1; x = ++n; /* sets x and n to 6 */ 1. n = n + 1; 2. x = n;
BBS Yapısal Programlama (Structured Programming)15 /* Preincrementing and postincrementing*/ #include int main(void) { int c; c = 5; printf(“%d\n”,c); printf(“%d\n”, c++); printf(“%d\n\n”,c); c = 5; printf(“%d\n”, c); printf(“%d\n”, ++c); printf(“%d\n”, c); return 0; }
BBS Yapısal Programlama (Structured Programming)16 /*** increment and decrement expressions ***/ #include int main(void) { int a =0, b = 0, c = 0; a = ++b + ++c; printf(“\n%d %d %d”, a,b,c); a = b++ + c++; printf(“\n%d %d %d”, a,b,c); a = ++b + c++; printf(“\n%d %d %d”, a,b,c); a = b c; printf(“\n%d %d %d”, a,b,c); return 0; }
BBS Yapısal Programlama (Structured Programming)17 Arithmetic Assignment Operators Assume: int c = 3, d = 5, e = 4, f = 6, g = 12; Operator ExpressionExplanation Assigns +=c += 7 c = c to c -= d -= 4 d = d to d *= e *= 5 e = e * 5 20 to e /= f /= 3 f = f / 3 2 to f %= g %= 9 g = g % 9 3 to g
BBS Yapısal Programlama (Structured Programming)18 Exercise 1 Given int i=2, j = 3, k =4; Evaluate the following: 1.k *= i – j; 2.i += j = k; 3.j -= -k i;
BBS Yapısal Programlama (Structured Programming)19 Exercise 2 Determine the value of the following expressions: * (2 * 2 – 2) % 2 / *( ( 8 + 7) % 6) + 5 * 4 % 3 * * (float) 6 / 5
BBS Yapısal Programlama (Structured Programming)20 Exercise 3 1.Given double x; int y = 4; What is the result of the following? x = y/3+(y-1.0)/y; 2.Given double x = 2.5; What is the value of x? x = (int)x*3+x;
BBS Yapısal Programlama (Structured Programming)21 Programming Exercises 1.Write a program that reads a four digit number and finds the sum of individual digits. 2.The distance d between points (x 1, y 1 ) and (x 2, y 2 ) is given by d = ( ( x 1 – x 2 ) 2 + (y 1 – y 2 ) 2 ) ½ Write a program that reads the coordinates of the two points and calculates the distance between them