140 likes | 424 Views
Operators and Expressions. Rohit Khokher. Operators and Expression. Arithmetic Operators. #include <stdio.h> void main () { int month, days; days = 45; month = days /30; days = days % 30; printf (“Month = %d Days = %d”, months, days) }. month = 45/30 = 1
E N D
Operators and Expressions RohitKhokher
Arithmetic Operators #include <stdio.h> void main () { int month, days; days = 45; month = days /30; days = days % 30; printf (“Month = %d Days = %d”, months, days) } month = 45/30 = 1 days = 45 % 30 = 15
Real & mixed mode arithmetic #include <stdio.h> void main () { float x , y, z; int a, b, c; x=6.0; y=7.0; a=6; b=7; z= x/y; printf (“Z = %d\n”, z); c=a/b; printf (“C = %d\n”, c); } #include <stdio.h> void main () { float x , y, z; int a, b, c; x=6.0; y=7.0; a=6; b=7; z= a/y; printf (“Z = %d\n”, z); c=x/b; printf (“C = %d\n”, c); } z=6.0/7.0 =0.857143 z=6/7.0 =0.857143 c=6/7=0 c=6.0/7=0 Note: If one operand is float then the result is float.
Assignment Increment Decrement y=++m (prefix) increase the value of m and assign it to y. y=m++ (postfix) assign the value m to y then increment m.
Conditional Operators Expression 1 ? Expression 2 : Expression 3 If expression 1 is true then the value is expression 2 else the value is expression 3 If a> b then x=a else x=b x=(a>b) ? a : b;
Bitwise operators Bitwise Manipulating data at bit level The comma operators x=10; y=5; v= x + y; v = (x=10,y=5, x + y);
Arithmetic Expression C expression Algebraic expression
Example #include <stdio.h> void main () { float a, b, c, x ,y, z; a=9; b=12; c=3; x=a-b/3+c*2-1; y=a-b/(3+c)*(2-1); z= a-(b/(3+c)*2)-1; printf (“x= %f\n”, x); printf (“x= %f\n”, y); printf (“x= %f\n”, z); } x= 9 – 12 / 3 + 3 * 2 - 1; 4 6 5 11 10 y= 9 – 12 / (3+3) * (2-1); 6 1 2 2 z = 9- (12 / (3+3) * 2) -1; 7 6 2 4 5 4
Precedence of Arithmetic operators An arithmetic expression is evaluated from left to right. High priority operators ( * / % ) are evaluated first. Low priority operators ( + - ) are evaluated next. 10 11 5 4 6 x= 9 – 12 / 3 + 3 * 2 - 1;
Parenthesized expression The parenthesized expressions change the evaluation priority. The innermost parentheses have the highest priority. The outermost parentheses have the lowest priority. The nesting levels of intermediate parentheses determine their priority. Un-parenthesized expressions within parentheses are evaluates according to the rules described for un parenthesized expressions. z = 9 - ( 18 / (3 + 3 * 2 ) * 2 ) - 1; 6 9 2 4 5 4
Type conversions in expressions Implicit Type Conversion Explicit Type Conversion (Type name) expression (int) a+b (double) x/y • short and char are automatically converted to int. • If one operand is long double then the other operand is converted to long double and the result is in long double. • else If one operand is double then the other operand is converted to double and the result is in double. • else If one operand is float then the other operand is converted to float and the result is in float. • else If one operand is unsigned long int then the other operand is converted to unsigned long int and the result is in unsigned long int . • else If one operand is unsigned long int then the other operand is converted to unsigned long int and the result is in unsigned long int . • else If one operand is long int and the other is unsigned int, then • If unsigned int can be converted to long int, then the result will be long int • else both will be unsigned long int. • else If one operand is long int then the other operand is converted to long int and the result is in long int . • else If one operand is unsigned int then the other operand is converted to unsigned int and the result is in unsigned int . Operator Associativity Decides the order in which multiple occurrences of the same level operators are applied.