Operator Precedence in C

A single expression in C may have multiple operators of different types. The C compiler evaluates its value based on the operator precedence and associativity of operators.

The precedence of operators determines the order in which they are evaluated in an expression. Operators with higher precedence are evaluated first.

For example, take a look at this expression −

x =7+3*2;

Here, the multiplication operator “*” has a higher precedence than the addition operator “+”. So, the multiplication 3*2 is performed first and then adds into 7, resulting in “x = 13”.

The following table lists the order of precedence of operators in C. Here, operators with the highest precedence appear at the top of the table, and those with the lowest appear at the bottom.

CategoryOperatorAssociativity
Postfix() [] -> . ++ – –Left to right
Unary+ – ! ~ ++ – – (type)* & sizeofRight to left
Multiplicative* / %Left to right
Additive+ –Left to right
Shift<< >>Left to right
Relational< <= > >=Left to right
Equality== !=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logical AND&&Left to right
Logical OR||Left to right
Conditional?:Right to left
Assignment= += -= *= /= %=>>= <<= &= ^= |=Right to left
Comma,Left to right

Within an expression, higher precedence operators will be evaluated first.

Operator Associativity

In C, the associativity of operators refers to the direction (left to right or right to left) an expression is evaluated within a program. Operator associativity is used when two operators of the same precedence appear in an expression.

In the following example −

15/5*2

Both the “/” (division) and “*” (multiplication) operators have the same precedence, so the order of evaluation will be decided by associativity.

As per the above table, the associativity of the multiplicative operators is from Left to Right. So, the expression is evaluated as −

(15/5)*2

It evaluates to −

3*2=6

Example 1

In the following code, the multiplication and division operators have higher precedence than the addition operator.

The left−to−right associativity of multiplicative operator results in multiplication of “b” and “c” divided by “e“. The result then adds up to the value of “a“.

#include <stdio.h>intmain(){int a =20;int b =10;int c =15;int d =5;int e;
   e = a + b * c / d;printf("e : %d\n",  e );return0;}

Output

When you run this code, it will produce the following output −

e: 50

Example 2

We can use parenthesis to change the order of evaluation. Parenthesis () got the highest priority among all the C operators.

#include <stdio.h>intmain(){int a =20;int b =10;int c =15;int d =5;int e;

   e =(a + b)* c / d;printf("e:  %d\n",  e);return0;}

Output

Run the code and check its output −

e: 90

In this expression, the addition of a and b in parenthesis is first. The result is multiplied by c and then the division by d takes place.

Example 3

In the expression that calculates e, we have placed a+b in one parenthesis, and c/d in another, multiplying the result of the two.

#include <stdio.h>intmain(){int a =20;int b =10;int c =15;int d =5;int e;
   e =(a + b)*(c / d);printf("e: %d\n",  e );return0;}

Output

On running this code, you will get the following output −

e: 90

Precedence of Post / Prefix Increment / Decrement Operators

The “++” and “− −” operators act as increment and decrement operators, respectively. They are unary in nature and can be used as a prefix or postfix to a variable.

When used as a standalone, using these operators in a prefix or post−fix manner has the same effect. In other words, “a++” has the same effect as “++a”. However, when these “++” or “− −” operators appear along with other operators in an expression, they behave differently.

Postfix increment and decrement operators have higher precedence than prefix increment and decrement operators.

Example

The following example shows how you can use the increment and decrement operators in a C program −

#include <stdio.h>intmain(){int x =5, y =5, z;printf("x: %d \n", x);

   z = x++;printf("Postfix increment: x: %d z: %d\n", x, z);

   z =++y;printf("Prefix increment. y: %d z: %d\n", y ,z);return0;}

Output

Run the code and check its output −

x: 5 
Postfix increment: x: 6 z: 5
Prefix increment. y: 6 z: 6

Logical operators have left−to−right associativity. However, the compiler evaluates the least number of operands needed to determine the result of the expression. As a result, some operands of the expression may not be evaluated.

For example, take a look at the following expression −

x >50&& y >50

Here the second operand “y > 50” is evaluated only if the first expression evaluates to True.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *