2 Types Operators and Expression 2 1 Variable
2 Types, Operators and Expression
• • • 2. 1 Variable Names 2. 2 Data types and Sizes 2. 3 Constants 2. 4 Declarations 2. 5 Arithmetic Operators 2. 6 Relational and Logical Operators 2. 7 Type Conversions 2. 8 Increment and Decrement Operators 2. 9 Bitwise Operators 2. 10 Assignment Operators and Expressions 2. 11 Conditional Expressions 2. 12 Precedence and Order of Evaluation
2. 1 Variable Names Name rules: 1. Names are made up of letters , digits , underscore( _ ). 2. The first character must be a letter or an underscore. 3. Upper and lower case letters are distinct. 4. A name may have the number of characters: MS C: The first 8. Turbo C: The first 32. 5. Can’t use keywords (int , float , if , else).
You’d better do with these rules: 1. Lower case for variables, upper case for symbolic constants. 2. Don’t use underscore as a first character. 3. Choose the meaningful names. 4. All the names used in a C program for constants, variables, functions.
E. g. √ E. g. × average Dennis M. Ritchie Comm 1 $23. 4 comm 1 3 d _some a>b Li_Ping while
2. 2 Data types and sizes dependent Machine • int 16 bits • short 16 bits • long 32 bits -32768 ~32767 -231 ~ (231 -1) • unsigned long 32 bits 0~ 232 -1 • unsigned int 0~65535 16 bits
• char 8 bits -128~127 • unsigned char 8 bits 0~255 • float 32 bits 6~7 decimal digits • double -1038~1038 < 10 -38 ≈ 0 64 bits -10308~10308 15~16 decimal digits < 10 -308 ≈ 0 • long double • limits. h • float. h usually same as double
2. 3 Constants ◆ Symbolic Constants #define name replacementtext #define PI 3. 1415926 main() { double r, s, p; scanf(〞%lf 〞, &r); p=2*PI*r ; s=PI*r*r ; printf(〞p=%f, s=%fn〞, p , s); }
• Integer Constants : • decimal-- • int • long • unsigned long • octal-- • a leading 0 • hexadecimal-- • a leading 0 x or 0 X -123 100 l(L) 12 u(U) 150 ul(UL) 037 0 x 1 f
• Real Constants : • a decimal point 123. 4 • an exponent 6. 02 e 23 • float constant 12. 5 f(F) • long double 12. 5 l(L) 6. 02 e 23 f
• Character Constants : • General----------′x′, ′ 0′ , ′*′ • Escape sequence----′n′, ′t′, ′ ′ a bell character \ backslash b backspace ? question mark f form feed ′ single quote n new-line 〞 double quote r carriage return ooo octal number t horizontal tab xhh hexadecimal v vertical tab ′141′ ′x 61′ ′a′ null character
2. 4 Declaration ◆ Variables must be declared before they are used. int lower, upper, step; char c; int lower; int upper; int step; char c;
◆ Variables may also be initialized in its declaration. #define MAXLINE 100 int i = 0; int limit = MAXLINE+1; float eps = 1. 0 e-5; char esc = '\';
◆ The const qualifier says that the elements will not be altered. const double e = 2. 7182845905; printf(%lfn”, e); e=2. 71828; ×
◆ An auto variable is initialized each time, there is no explicit initializer. For the automatic variables: main() { int x; x=1; x++; printf("x=%dn", x); main() } } 21 { int x; x++; printf("x=%dn", x); ?
Discussion • 1. For the variable’s declaration, legal or illegal? • 1) float if ; 2) real x, y; • 3) integer i ; j ; k ; 4) character c 1, c 2 ; • 2. Write out the length of the character string. • 1) 〞\ 33 〞 2) 〞 mn〞 • 3) 〞a 〞 4) 〞 \033 l 〞 • 3. Write out the result of the program. • main() • { int x=-1; • printf(〞x=%dt%ot%x%un 〞, x, x); • }
-1 D 100000001 B 111111110 B 11111111 B 177777 O FFFF H 65535 U
2. 5 Arithmetic Operators • Precedence - (unary) • * / % • + • Eg. • 1/2=0 3/2=1 1. 0/2=0. 5 • 8%3=2 2%5=2 3. 0%2( ) • -10/3=-3 -10%3=-1
2. 6 Relational and logical Operators R. O. <, <=, >, >= = =, != E. g. 3>=3 1 ′x′< ′A ′ 0 12. 56!=23. 8 1 1. 0/3*3==1. 0 1
L. O. ! && || (unary) E. g. x>=0 && y!=0 x<0 || y==0 !x if (x==0) if ( !x ) if (x!=0) if ( x )
int a=0, b=0; ++a && ++b; printf(“a=%d, b=%dn”, a, b); a=1, b=1 int a=0, b=0; ++a || ++b; printf(“a=%d, b=%dn”, a, b); a=1, b=0 int a=-1, b=0; ++a && ++b; printf(“a=%d, b=%dn”, a, b); a=0, b=0 int a=-1, b=0; ++a || ++b; printf(“a=%d, b=%dn”, a, b); a=0, b=1
Exercise Write out the expressions: x%10==y%10 1. The lowest bit of x and y is same. x==0||y==0 2. At least one is 0 for x and y. 3. The lowest bit of x is equal to its ten’s digit. x%10==x/10%10 4. Quadratic equation ax 2+bx+c=0 has not real root b*b-4*a*c<0 5. The point (a, b) is on the parabola y=x 2+3 x+5. b==a*a+3*a+5 6. x and y are all even numbers. x%2==0&&y%2==0
Exercise • Write out the expressions: 1. x [a, b) x>=a&&x<b 2. x [a, b) !(x>=a&&x<b) 3. x is less than 1000 and non-negative odd number. x<1000&&x>=0&&x%2!=0 4. The point (x, y) is in the unit circle x*x+y*y<1 x 2+y 2<1.
• A year is a leap year if it is divisible by 4 but not by 100, except that years divisible by 400 are leap years. if ( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ) printf("%d is a leap yearn", year); else printf("%d is not a leap yearn", year);
Last Lecture • Data Type: int , long , float, double , char • Constant: 12, 056, 0 xea, 34 L, 3. 14 f , 6. 28 , 4. 6 e-8 'A', 't' , '107' , 'x 48' " abcd " • Variable: Name rules declaration • Arithmetic Operator + - , * / %, + • Relational Operator >= <= > < == != • Logical Operator ! && ||
2. 7 Type Conversions 1. Arithmetic operation wider double ← float unsigned long unsigned narrower int ← short, char
• E. g. int i; float f; double d; long e; 10 +′a ′+ i * f – d / e i d d
2. Assignment statement converted to the type of the left. int i=97; float f=3. 14; char c= 'A'; int i=20; i = c; f = i; c = i; i = f;
3. Function call , actual argument is passed to formal argument: char, short to int ; float to double (p 251) sqrt( 123 ) 4. Explicit type conversion cast (type name ) expression (float)(3*5/2+7%6) (float)5/9*(fahr-32)
2. 8 Increment and Decrement Operators • • ++ -- adds 1 to its operand subtracts 1 • prefix ++n • postfix n++ --n n-- • notes: operand must be variable (i+j)++ × -i++ is equivalent to -(i++) not (-i)++
Increments n after or before its value is used. • • n=5; x=n++; → x=n; n=n+1; → x=5; n=6; n=5; x=++n; → n=n+1; x=n; → x=6; n=6;
i=2; s=(i++)+(i++) ; i=5 s=6 i=2; s=(++i)+(++i); i=5 s=15
No value is wanted, just the incrementing effect. if ( a > 0 ) if ( a>0 ) n++; ++n;
Exercise • double x=1, y=2; int a; • After the statements were executed , what are the value of x, y and a? x=2, y=1, a=1 1. a=(++x)&&(- -y) 2. a=(- -x)&&(- -y) x=0, y=2, a=0 3. a=(++x)||(- -y) x=2, y=2, a=1 x=0, y=1, a=1 4. a=(- -x)||(- -y)
2. 9 Bitwise Operators ~ & ^ | << >> one’s complement bitwise AND bitwise exclusive OR bitwise inclusive OR left shift right shift
Eg. a=1101010110010101 b=11110000 a 1101010110010101 ~a 0010101001101010 a&b a|b a^b 1101010110010101 & 11110000 110100000000 reset(mask off) 1101010110010101 | 00001111 110101011111 set(turn on) 1101010110010101 ^ 00001111 110101101010 invert
a=0000000100 a<<2 00000010000 (4)10 (16)10 • shifts the value of a left by two position, filling vacated bits with zero. • right shifting an unsigned quantity fills vacated bits with zero. a>>2 000000001 (1)10
• right shifting a signed quantity will fill with sign bits on some machines and with 0 -bits on others. int a=-4; a>>2 1000000100 1111111011 111111100 11111111 100000000 100000001 (-1)
The difference between &, | and &&, || : if x=1, y=2, then x&y x&&y x||y 01&10 1&&2 01|10 1||2 (00)2 (11) 2 0 1 3 1
Exercise • Write out the expressions: • 1. Whether x’s 5 th bit is 1? • 2. x is equal to setting its 5 th bit to 1, other bits don’t be changed. 00100000 x&32 !=0 x==x|32
2. 10 Assignment Operators and Expression A. O v=e E. g. int a, b; a=10 ; b=a+6; v op = e += E. g. -= *= v op (e) /= %= <<= >>= &= |= ^= int a=3; a+=10 ; a=a+10;
int a=12; What is the value of a? (1) a-=2 a=a-2 10 (2) a*=2+3 a=a*(2+3) 60 (3) a/=a+a a=a/(a+a) 0 (4)a+=a-=a*=a a=a*a 144 a=a-144 0 a=a+0 0
2. 11 Conditional Expressions e 1? e 2: e 3 E. g. y= x>0? x: -x (ternary operator ? : ) Notes: 1. The precedence of ? : is upper than assignment 2. The type of result is determined by the conversion rules 3. The associativity of ? : is R to L a>b? a: c>d? c: d means a>b? a: (c>d? c: d)
if (a>b) z=a; else z=b; z=(a>b)? a: b; if (a>b) printf(“%dn”, a); else printf(“%dn”, b); a>b? printf(“%dn”, a): printf(“%dn”, b);
Exercises • • Write out the expressions. 1. |x-y| x>y? x-y: y-x 2. max(a, b) a>b? a: b 3. sign(x) x>0? 1: -1
2. 12 Precedence and Order of Evaluation Operators Associativity () [] ->. left to right ! ~ ++ -- + - * (type) sizeof right to left */% left to right +- left to right << >> left to right < <= > >= left to right == != 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
Exercise What are the values of expressions and variables? int a=1, b=2, c=3; 1. a==1||b==2&&c!=3 1 a: 1 b: 2 c: 3 0 a: 1 b: 2 c: 3 2. (a==1||b==2)&&c!=3 1 a: 1 b: 2 c: 3 3. a==1||(b==2&&c!=3) 1 a: 2 b: 1 4. (a=2)&&(b=1) 3 a: 2 5. a=2, 3 3 a: 3 6. a=(2, 3) 10 a: 2 b: 10 7. a+=c==3, b*=5 11 a: 11 b: 10 c: 3 8. a+=(c==3, b*=5)
Brief summary • 1. standard data types int , float, double, char • 2. operators and expressions • arithmetic + , - , * , / , % • relational < , <= , >= , == , != • logical && , || , ! • bitwise & , |, ^, ~ , << , >> • assignment = , += , -= , &= , <<=, . . . • conditional ? : • 3. library functions printf(), scanf(), putchar(), getchar()
- Slides: 50