Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 02 Data Types, Conversions if Statements METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan.

Similar presentations


Presentation on theme: "Lecture 02 Data Types, Conversions if Statements METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan."— Presentation transcript:

1 Lecture 02 Data Types, Conversions if Statements METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Wed Jul 3, 2002

2 Today... Comments Reserved Words: what cannot be used as a variable name. More on Data Types: sizes, ranges Type conversion Characters

3 more Today... if statements if - else Exercises Programming a simple calculator

4 Comments anything between ‘/*’ and ‘*/’ are ignored. So, you can type your comments, poems, explanations, reminders enclosed within /* and */. Thus: /*this is a comment*/ int /*hahaha!*/ x =/**/ 5; is same as: int x=5;

5 Reserved Words autodoubleintstruct breakelselongswitch caseenumregistertypedef charexternreturnunion constfloatshortunsigned continueforsignedvoid defaultgotosizeofvolatile doifstaticwhile

6 Data-typeSizeRange char1 bytes-128 to 127 unsigned char1 bytes0 to 255 short2 bytes-32768 to 32767 unsigned short2 bytes0 to 65535 int4 bytes-2147483648 to 2147483647 unsigned int4 bytes0 to 4294967295 long4 bytes-2147483648 to 2147483647 unsigned long4 bytes0 to 4294967295 float (6 digits)4 bytes 1.175494e-38 to 3.402823e+38 double (15 digits)8 bytes 2.225074e-308 to 1.797693e+308

7 Type Conversion conversions among different data types may occur while evaluating an expression. int x=3.4, y=4; float z = x; char c = z; Two type of conversions: –Implicit (automatic) –explicit (type-cast)

8 Implicit (automatic) Type Conversion If operands are of the same type, the result will be of this type too float x,y,z; z = x+y; /*x+y is also float*/ If operands are of different types, they will be converted to the “broader” type (i.e., int to float) before the calculation int x; float y, z; z = x+y; /*x+y is float*/

9 Beware! short s; int i; long l; float f; double d; Safe conversion (to broader type) l=i; l=s; i=s; d=i; f=i; /*100 converted to 100.0 d=f; /*?*/ Unsafe conversion (loosing information) i=f; /* truncation: 3.14 to 3 */ s=l; /* dropping bits */ f=d; /* truncation/rounding */

10 Arithmetic Exercise int x=9; float f, k=2; x = (x/2 + 0.5); /* x=? */ x += (x%5 + 1); /* x=? */ x = (x/2.0 + 0.5); /* x=? */ f = 5/2 + 5/k + k/0.5; /*f=?*/

11 Explicit Conversion (type-cast) Most compiler give warnings for possibly unsafe conversions. One type of data can be explicitly forced into another data type. float f=4.3; int x, y=2; x=3.7; /*compiler warning*/ x=(int) f; /*ok*/ f = x/(float)y;

12 Characters Character constants are given in single quotes. ‘a’, ‘A’, ‘&’ are characters. There are special characters like: ‘\n’, ‘\0’, ‘\t’ Character constants are represented numerically from 0 to 255. char c = ‘e’; int x = c – ‘b’; /* x = ? */

13 Operators in C Unary Binary Assignment comma

14 Unary Operators sizeof(i) : the number of bytes of storage allocated to i +123 : positive 123 -123 : negative 123 ~i : one's complement (bitwise complement) !i : logical negation (i.e., 1 if i is zero, 0 otherwise) *i : returns the value stored at the address pointed to by i &i : returns the address in memory of i

15 unary Examples a = 00000110 b = 00000011 a | b = 00000111 a &b = 00000010 j=3; /* j=00000011 */ k=j<<2; /* k=00001100 */ /*k=12*/ m=j>>2; /*m=00000000 */ /*m=0*/

16 more Unary Operators ++i : adds one to i, and returns the new value of i --i : subtracts one from i, and returns the new value of i i++ : adds one to i, and returns the old value of i i-- : subtracts one from i, and returns the old value of i i[j] : array indexing i (j) : calling the function i with argument j i.j : returns member j of structure i i->j : returns member j of structure pointed to by i

17 Binary Operators + : addition - : subtraction * : multiplication / : division % : remainder (e.g., 2%3 is 2), also called 'modulo' << : left-shift (e.g., i<<j is i shifted to the left by j bits) >> : right-shift & : bitwise AND | : bitwise OR ^ : bitwise exclusive-OR

18 more Binary Operators && : logical AND (returns 1 if both operands are non-zero; else 0) || : logical OR (returns 1 if either operand is non-zero; else 0) < : less than (e.g., i<j returns 1 iff i is less than j) > : greater than <= : less than or equal >= : greater than or equal == : equals != : does not equal ? : conditional operator

19 Assignment Operators x = is same as x = x = assignment += addition assignment -= subtraction assignment *= multiplication assignment /= division assignment %= remainder/modulus assignment &= bitwise AND assignment |= bitwise OR assignment ^= bitwise exclusive OR assignment <<= left shift assignment >>= right shift assignment

20 Comma operator multiple expressions can be separated by comma. The expressions are evaluated in right to left order, and the value of the overall expression is equal to that of the rightmost expression. x = ((y=3, 2)); /* y=3; x=2; */ x = ((y=3, y+1)); /* y=3; x=4; */

21 evaluation order of Operators precedence: the order in which operators are applied. the higher precedence operators are applied first. associativity: the order in which operators of the same precedence are applied.

22 OperatorAssociativity () [] ->>.left-to-right -+ (unary) ++ -- ! ~ * & sizeof (type)right-to-left * / %left-to-right + - (arithmetic)left-to-right >left-to-right >=left-to-right == !=left-to-right & ^ | &&left-to-right ||left-to-right ?:right-to-left = += -= *= /= %= &= ^= |= >=right-to-left,left-to-right highest precedence lowest precedence

23 int x, y=0; x = ++y; /* x = ?, y = ? */ x = y++; /* x = ? y = ? */ x = (++y); /* x = ? y = ? */ x = (y++); /* x = ? y = ? */ Question...

24 Expressions, Statements Expression: consists of operands (variables or constants) and operators: –Relational: x==0, x<y, x!=10 –Arithmetic: x+2, x++, 2*5 –Assignment: x=5, y=x Statements: –declarative: int x; –simple: x=3; –compound: { x*=4; printf(“%d”, x); } –conditional: if, if-else, switch –controlling (loops): while, do-while, for, (goto)

25 Programming Goal Simple Calculator: Write a program that reads two integers separated with one of the four arithmetic operators, and prints out the result of this arithmetic expression.

26 If Statement if( ) –test can be a relational expression, or any integer-valued expression. –statement can be simple: one-statement ending with one-semicolon. compound: block of statements surrounded with {...} or, the statement can be another if-statement.

27 A note on relational statements 0 (zero) means ‘false’, and any number other than zero means ‘true’. also, a conditional truth evaluates to a numerical 1, whereas falsity evaluates to 0. x = (3==2); /* x = ? */ x = (4>-5); /* x = ? */

28 if examples if(sayi==10) printf(“sayi 10’a esittir.”); if(!(sayi<10)) printf(“sayi >=10 dur.”); if(sayi) printf(“sayi 0 degildir.”);

29 int x=3; if(x != 3) printf(“t”); printf(“a”); if(x % 2){ printf(“hm”); if(x>0){ printf(“e”); } if(x-3) if(x==3) printf(“s”); printf(“t”); Find the output of the following:

30 if-else statement if ( ) else if(x==y) printf(“x is equal to y”); else print(“x is NOT equal to y”);

31 you already know: (if... else if...) if(x==y) printf(“x is equal to y”); else if(x>y) printf(“x is greater than y”); else printf(“x is smaller than y”);.

32 Tip for if statements: if ( ) To solidify the scope of the if statements, follow the procedure. –Look at right after the ‘)’ that closes the. if there’s a ‘{‘, then the scope of the ‘if’ ends at the corresponding ‘}’. if not, –search for the first semicolon ‘;’ after the ‘)’. The semicolon will determine the end of the if- statement.

33 exercises (a) #include int main() { int gender = 2; gender--; if (Gender == 1) printf("The student is male\n"); else printf("The student is female\n"); return 0; }

34 exercises (b) #include int main() { int gender = 2; gender--; if (gender == 1) printf("The student is male\n"); else; printf("The student is female\n"); return 0; }

35 exercises (c) #include int main() { int gender = 2; if (--gender == 1) printf("The student is male\n"); else printf("The student is female\n"); return 0; }

36 exercises (d) #include int main() { int gender = 2; if (gender-- == 1) printf("The student is male\n"); else printf("The student is female\n"); return 0; }

37 exercises (e) #include int main() { int gender = 2; gender--; if (gender == 1); printf("The student is male\n"); else printf("The student is female\n"); return 0; }

38 exercises (f) #include int main() { int gender = 2; gender = ((gender / 2) % 2) + 0.9; if (gender == 1) printf("The student is male\n"); else printf("The student is female\n"); return 0; }

39 Programming Homework Simple Calculator: Write a program that reads two integers separated with one of the four arithmetic operators, and prints out the result of this arithmetic expression.


Download ppt "Lecture 02 Data Types, Conversions if Statements METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan."

Similar presentations


Ads by Google