June 07, 2018

Operator Precedence and Associativity in C


   Operator precedence determines which operator is performed first in an expression with more than one operators with different precedence; for example, the multiplication operator has a higher precedence than the addition operator.

For example,

x = 5 + 2 * 4;

here, x is assigned 13, not 28 because operator * has a higher precedence than +, so it first gets multiplied with 2*4 and then adds into 5.


Example


If two operators sharing an operand have the same precedence, they work according to their associativity. Associativity is either Left to Right or Right to Left. It is the order in which an operator works with its operands. 

For example, * (multiplication) and / (division) have same precedence and their associativity is Left to Right, so the expression "20 / 2 * 2" is treated as "(20 / 2) * 2".



In C, each operator has a fixed priority or precedence in relation to other operators. As a result, a higher precedence operator is evaluated before a lower precedence operator. If the precedence levels of operators are the same, then the order of evaluation depends on their associativity (or, grouping). The following table lists operator precedence and associativity.


Operator Precedence and Associativity
Operator Precedence and Associativity


Here are some more examples:


Example 1:

25 + 20 / 5 - 10

Example 1



Here / operator has higher precedence than + and -, so 20/5 will be evaluated first. The operators + and - have the same precedence and associates from left to right therefore in our expression 25 + 20/5 - 10 after division, addition(+) will be performed before subtraction (-) .


The following program demonstrates this example.

#include <stdio.h>

int main(void)
{

   int x;
  
   x = 25 + 20 / 5 - 10;

   printf("x = %d", x);

   return 0;
}


Output:

x = 19


Example 2:

2 + 6 < 9

Example 2

Here + operator has higher precedence than <, so 2+6 will be evaluated first and then < operator will be evaluated. 


The following program demonstrates this example.

#include <stdio.h>

int main(void)
{

   int x;
  
   x = 2 + 6 < 9;

   printf("x = %d", x);

   return 0;
}


Output:

x = 1


Example 3:

10 - 5 + 4 / 2 < 4 + 2


Example 3


Here / operator has higher precedence hence 4/2 will be evaluated first. Then + and - operators have same precedence and associates from left to right, therefore in our expression 10 - 5 + 4 / 2 < 4 + 2 after division, - will be evaluated before + . Since precedence of < operator is lower than that of /, + and - hence it will be evaluated at last.


The following program demonstrates this example.

#include <stdio.h>

int main(void)
{

   int x;
  
   x = 10 - 5 + 4 / 2 < 4 + 2;

   printf("x = %d", x);

   return 0;
}


Output:

x = 0


Role of Parentheses in Evaluating Expressions

If we want to change the order of precedence of any operation, we can use parentheses. According to the parentheses rule, all the operations that are enclosed within parentheses are performed first. For example:

20 / 2 + 3

Here division(/) will take place before addition(+) according to precedence rule, but you can change this behavior by using parentheses.

20 / (2 + 3)

Now + will be evaluated first followed by /.


The following program demonstrates this example.

#include <stdio.h>

int main(void)
{

   int x, y;
  
   x = 20/2+3; /* Division will take place before addition. */
   y = 20/(2+3); /* Addition will take place before division. */

   printf("x = %d\n", x);
   printf("y = %d", y);

   return 0;
}


Output:

x = 13
y = 4


You can also nest parentheses like this:

(46-(32+(2+2))) / 5

In such cases expressions inside the innermost parentheses are evaluated first, then the next innermost parentheses and so on.



next    Type Conversion in C
top    Index
prev    Assignment Operator in C



No comments:

Post a Comment