Working with operators can be confusing, especially when we are working with multiple operators simultaneously. So to avoid any misunderstanding, we need to know Operator Precedence and Associativity work in C.

Operator precedence controls how terms in an expression are grouped and how an expression is evaluated. Certain operators take precedence over others. The multiplication operator, for example, takes priority over the addition operator.

For example, x = 2 + 3 * 5;

Here, the value of x will be assigned as 17 and not 20. The “ * ”operator has higher precedence than the “ + ” operator. So the first 3 is multiplied by 5 to get 15, and then 2 is added to 15 to result in 17.

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

Operator Associativity in C

The direction in which an expression is evaluated is determined by the associativity of operators. Associativity is utilized when two operators of the same precedence exist in an expression. Associativity can be either left to right or right to left.

For example, consider x = 5 / 3 * 3;

Here, the value of x will be assigned as 3 and not 5. ‘*’ operator and ‘/’ operator have the same precedence, but their associativity is from Left to Right. So first 5 is divided by 3 to get 1, and then 1 is multiplied by 3, resulting in 3.

Key Points About Operator Precedence and Associativity in C.

When two or more operators have the same precedence, associativity is utilized.

Example

#include <stdio.h>
int main()
{
printf("Output of 1 == 2 != 3 is %d",1 == 2 != 3);
return 0;
}

Output:

Output of 1 == 2 != 3 is 1

In this case, the operators ‘==’ and ‘!=’ have the same priority. And they are related from left to right. As a result, 1 == 2 is executed first.

All operators with the same precedence have the same associativity.

This is required because otherwise, the compiler would be unable to determine the evaluation order of phrases with two operators with the same precedence but differing associativity. For example, the associativity of + and – is the same.

Comma has the least precedence.

The comma has the least precedence among all operators and should be used with caution.

Example

#include <stdio.h>
int main()
{
int a;
a = 10, 20, 30;
printf("%d", a);
return 0;
}

Output:

10

Here /the expression is Evaluated as (a = 1), 2, 3.

Postfix Increment and Prefix Increment have different Precedence and Associativity.

Postfix ++ has more precedence than prefix ++, and their associativity is also different. The associativity of postfix ++ is left to right, while that of prefix ++ is right to left.

No comparison chaining of operators in C

In Python, an expression such as "c > b > a" is handled as "c > b and b > a," while in C, this sort of chaining does not occur.

Example

#include <stdio.h>
int main()
{
int x = 1, y = 2, z = 3;
if (x > y > z)
printf("True");
else
printf("False");
return 0;
}

Output:

False

Here (x > y > z) is treated as ((x > y) > z) because associativity of '>' is from left to right. Therefore the expression becomes ((30 > 20) > 10) which becomes (1 > 10) which results in false condition.

In this table, operators with the highest precedence appear at the top, while those with the lowest appear at the bottom. Higher precedence operators in an expression will be evaluated first.

Which of the operators has higher precedence, the addition or subtraction?

The precedence of Addition and Subtraction arithmetic operators is the same. So, let’s say we have an expression with us which is 20 + 5 – 10. The evaluation of this expression would be (20 + 5) – 10 because the associativity will be left to right for both the operators – multiplication and division. In a similar case, the calculation of 25 – 10 would be 15 because the associativity would be from right to left here.

Which of the operators has higher precedence, multiplication or division?

The precedence of Division and Multiplication arithmetic operators is the same. So, let’s say we have an expression with us which is 6 * 3 / 20. The evaluation of this expression would be (6 * 3) / 20 because the associativity will be left to right for both the operators – multiplication and division. In a similar case, the calculation of 40 / 4 * 5 would be (40 / 4) * 5 because the associativity would be from right to left here.

Which operator has the lowest precedence?

Comma has the lowest precedence among all the operators.

Conclusion

In this blog, we have extensively discussed Operator Precedence and Associativity in C. Operator precedence dictates the order of evaluation of operators in an expression.