Jun 17, 2014

C Programming #16: Operator precedence, associativity and order of evalation

In this article there related and important topic will be covered
  1. Operator precedence
  2. Associativity
  3. Order of Evaluation.

Operator precedence and Associativity

Say in mathematics we are given with an expression.

(2 * 3 + 2) - 6 / 2

We know that in maths we use the formula BODMAS - Brackets Of Division or Multiplication and Addition or Subtraction. First thing inside brackets which is 2 * 3 + 2 => 6 + 2 => 8. Expression is now simplified to 8 - 6 / 2 => 8 - 3 => 5. This is called Operator precedence.

Take another example
8 - 2 + 1
Here subtraction and addition have same priority in such case evaluated from left to right. 8 - 2 + 1 => 6 + 1 => 7. This is called Associativity.

More details can be found in this link.

C adds one detail called Order of Evaluation. which is discussed in next part of this article.

Here is table that has Operator precedence and Associativity defined.


Operator
Description
Associativity
++ --
post-increment, post-decrement
left to right
++ --
+ -
!~
&
sizeof
pre-increment, pre-decrement
Unary + and Unary -
Logical not and bit-wise not
Address operator
Size of variable/expression
right to left
* / % 
Multiplication, division and remainder
left to right
+ -
Addition subtraction 
left to right
<< >>
bit-wise left shift, bit-wise right shift
left to right
< <=
> >=
Less than or less than or equal to
greater than or greater than or equal to
left to right
== !=
Equal, Not Equal
left to right
&
Bit-wise AND
left to right
^
Bit-wise XOR
left to right
|
Bit-wise OR
left to right
&&
Logical AND
left to right
||
Logical OR
left to right
?:
Conditional operator
right to left
=
Assignment, Other Assignment short cut
right to left
,
Comma
left to right

Order of Evaluation

for example we have

2 + 3 + 8

both (1 + 3) + 8 and 1 + (3 + 8) both will same result 12. Even though there is Associativity that tells left to right, the result would be same in both cases. In such cases C standard allows any order of evaluation. Hence order is not guaranteed as the result is same. You might not appreciate now, it becomes glaringly clear if number were some function (covered later).

Operator where the order of evaluation is not guaranteed (when one or more of same operator is used in same parenthesis level)
  1. Addition
  2. Multiplication
  3. Binary AND
  4. Binary OR
  5. Binary XOR

In all other operators the order of evaluation is guaranteed.

But when it comes to 2 - 3 + 8, it is always (2 - 3) + 8 which is 7. Not 2 - (3 + 8) !

Examples


#include <stdio.h>

int main() 
{
   int a, b, c;

   a = 2 + 3 * 2;
   b = 4 / 2 - 1;
   c = 10 / 2 % 3;
 
   printf("Value of a, b, c is %d, %d, %d\n", a, b, c);
 
   a = b = c = 1;

   printf("Value of a, b, c is %d, %d, %d\n", a, b, c);

   a = b = c == 2;

   printf("Value of a, b, c is %d, %d, %d\n", a, b, c);
 
   return 0;
}

output of the above program is


Value of a, b, c is 8, 1, 2
Value of a, b, c is 1, 1, 1
Value of a, b, c is 0, 0, 1

Links

Quiz - Not Yet written !! Next Article - C Programming #17: Comments
Previous Article -  C Programming #15: Operators - Expression and Statement
All Article - C Programming

No comments :

Post a Comment