09 - Operators in C' - PPTX
09 - Operators in C' - PPTX
09 - Operators in C' - PPTX
Operators in C
Nihar Ranjan Roy
Equal to
a <= b
a >= b
Arithmetic Operators
Operator name Syntax a=b a+b a-b +a -a Operator name
Logical operators
Syntax a && b a || b !a
Unary plus (integer promotion) Unary minus (additive inverse) Multiplication Division
a*b
Logical OR
Write a program for checking weather a given year is a leap year or not.
a/b
int yr; puts ("please enter an year"); scanf("%d",&yr); if((yr%4==0 && yr%100!=0)|| yr%400==0) puts("its a leap year"); else puts("its not a leap year");
6 6 7
10/19/2011
Bitwise operators
Operator name Syntax ~a a&b a|b a^b a << b a >> b Operator name
Other Operators
C first evaluates the condition. Based on the Function call a(a1, a2) return value of the condition the second or Comma a, b third operand is evaluated: Ternary/ conditional a?b:c If the condition is evaluated to true (1), only sizeof(a) the second operand (expression 1) is Size-of sizeof(type) evaluated. Then the operator return the value of the expression 1. Cast (type) a If the condition is evaluated to false (0), only the third operand (expression 2) is The ternary operator can be rewritten as evaluated. The value of the expression 2 is the if-else and return statement as follows: returned. if(condition) return expression1; int a=2,b=3,c; else c=(a>b)?a:b; //biggest of a & b return expression2; Syntax
72 0 1 0 0 1 0 0 0 184 1 0 1 1 1 0 0 0 8 0 0 0 0 1 0 0 0
Original
0 0 0 1 0 0 0 0
after shift 0 0 1 0 0 0 0 0
Nihar Ranjan Roy 5
printf("%d",c);
Operator Precedence
The precedence determines the order of binding in chained expressions, when it is not expressly specified by parentheses. For example,
a->b a.b
++x*3
It is ambiguous without some precedence rule(s). The precedence table tells us that: x is 'bound' more tightly to ++ than to *, so that whatever ++ does (now or latersee below), it does it ONLY to x (and not to x*3); it is equivalent to (++x, x*3).
10/19/2011
Operator Precedence
Precedence Operator ++ -() [] . ++ -+ ! ~ (type) & sizeof -> Description Suffix increment Suffix decrement Function call Array subscripting Element selection by reference Element selection through pointer Prefix increment Prefix decrement Unary plus Unary minus Logical NOT Logical bitwise NOT Type cast Address-of Size-of Associativity Precedence Operator
Operator Precedence..
Description Associativity
15
Left-to-right
13
Right-to-left
15
Ternary conditional Direct assignment Assignment by sum Assignment by difference Assignment by product Assignment by quotient Assignment by remainder Assignment by bitwise left shift Assignment by bitwise right shift Assignment by bitwise AND Assignment by bitwise XOR Assignment by bitwise OR Comma
Right-to-left
Left-to-right
11
Operator Precedence..
Precedence 3 4 5 6 7 8 9 10 11 12 Operator * / % + << >> < <= > >= == != & ^ | && || Description Multiplication division modulus (remainder) Addition subtraction Bitwise left shift Bitwise right shift For relational operators < respectively For relational operators respectively For relational operators > respectively For relational operators respectively For relational = respectively Bitwise AND Bitwise XOR (exclusive or) Bitwise OR (inclusive or) Logical AND Logical OR Nihar Ranjan Roy 10 Associativity
Evaluate
Evaluate
3*x++,
where though the post-fix ++ is designed to act AFTER the entire expression is evaluated, the precedence table makes it clear that ONLY x gets incremented (and NOT3*x); it is functionally equivalent to something like (tmp=3*x, ++x, tmp) with tmp being a temporary value.
Left-to-right
12
10/19/2011
Overflow
char a=255; a=a+1 printf("%d",a); Output? 0
Short int
int
Unsigned int
Long int
float
double
Long double
13
15
a=(float) b/c;
14