Binary operators in the same sub expression and at the same precedence level

# Binary operators in the same sub expression and at

This preview shows page 3 - 6 out of 7 pages.

Binary operators in the same sub expression and at the same precedence level are evaluated left to right (left associativity). Example: Consider the expression -a + ( c + b * ( c + a ) / c – b / a ) + a – b / 2 In the above expression the innermost parenthesis (c + a ) is evaluated first, then the next innermost parenthesis ( c + b * ( c + a ) / c – b / a ) is evaluated. In this subexpression, first b * ( c + a ) is evaluated then b * ( c + a ) / c is evaluated, then b / a is evaluated, then the whole expression is evaluated. Then –a is evaluated, then b / 2 is evaluated and finally the whole expression is evaluated. Compound assignment statements: C evaluates the assignment statements in two steps: The right-side expression is evaluated first, and then C stores the result at the address of the variable on the left. So, the statement x = x * 15 can be written in short form as x *= 15. Keep in mind that the variable on left is applied to the entire expression on the right; that is x *= 15 + y; is equivalent to x = x * (15 + y) ;. Compound assignment can be used with other arithmetic operators such as - , + , / , % . Writing Mathematical Formulas in C: Page 3 of 7 The formulas written in mathematics are different from what is written in C language . For example, the formula x = ab + cd in mathematics is written in C language as x = a*b + c*d where * indicates multiplication . Another example where division is considered x = d c b a + + is written in C language as x = (a+b) / (c+d) where / is division . Consider some more examples of mathematical formulas and their C language equivalents Mathematical Formula C Expression x 2 – 5bc x * x – 5 * b * c 2a 2 * a / ( 4 * b + 5 * c ) 4b + 5c In all the previous examples check for the use of parentheses , particularly when using the division. As the rule of evaluation of arithmetic expression says that the expression inside the parentheses is evaluated first, its use should be paid attention . Solved Problem#1: /******************************************************* Find output of following manually: ********************************************************/ #include<stdio.h> main() { int x=2; double a=2.0, b=7.0, c=1.0,y=3.0,z=5.0, result_1, result_2; // variable declarations result_1 = y/a; // calculations result_1 += 11.0; result_2 =(int) z %(int) y; printf (" The result_1 is : %lf \n",result_1); // print printf (" The result_2 is : %lf" , result_2); Page 4 of 7 } // end of main Sample Output: Solved Problem # 2:  #### You've reached the end of your free preview.

Want to read all 7 pages?

• Spring '10
• zaman
• Algebra, Expression
• • •  