Signup/Sign In

C Operators Precedence

There are so many operators in C language, and we can use multiple operators together as well. Do you know how we manage multiple operators present in a single expression? Do we perform addition first, or multiplication, how do we decide it?

Based on the Precendence of operators, the C compiler decides the order in which the operators are evaluated.

For example, if we have three variables a, b and c, then for the expression a+b*c, the compiler will first multiply b and c, and then add the result of the multiplication with a, because the * operator has higher precedence than the + operator.

Not all operators can be used together in a single expression, there are many such combinations that are treated as semantically invalid. For example, c = a < b ? a++ : a = b can not be parsed because of this rule. But many compilers ignore this and parse it as c = (((a < b) ? (a++) : a) = b), and then give an error.

The concept of operator precedence is only relevant for the operators that can be used together, and they are:

  1. Arithmetic operators

  2. Relational operators

  3. Logical operators

Order of Precedence

Following is the standard table of precedence of operators with 1 being the highest precedence.

Precedence

Operator

Description

Associativity

1

++, --

Suffix/postfix increment and decrement

Left-to-right

()

Function call

[]

Array subscripting

.

Structure and union member access

->

Structure and union member access through pointer

(type){list}

Compound literal(C99)

2

++, --

Prefix increment and decrement

Right-to-left

+, -

Unary plus and minus

!, ~

Logical NOT and bitwise NOT

(type)

Cast

*

Indirection (dereference)

&

Address-of

sizeof

Size-of

_Alignof

Alignment requirement(C11)

3

*, /, %

Multiplication, division, and remainder

Left-to-right

4

+, -

Addition and subtraction

5

<<, >>

Bitwise left shift and right shift

6

<, <=

For relational operators < and ? respectively

>, >=

For relational operators > and ? respectively

7

==, !=

For relational = and ? respectively

8

&

Bitwise AND

9

^

Bitwise XOR (exclusive or)

10

|

Bitwise OR (inclusive or)

11

&&

Logical AND

12

||

Logical OR

13

?:

Ternary conditional

Right-to-left

14

=

Simple assignment

+=, -=

Assignment by sum and difference

*=, /=, %=

Assignment by product, quotient, and remainder

<<=, >>=

Assignment by bitwise left shift and right shift

&=, ^=, |=

Assignment by bitwise AND, XOR, and OR

15

,

Comma

Left-to-right

Associativity

Operators that are in the same block (in the table above) are of the same precedence and thus they are evaluated in the given direction known as associativity. For example, the expression x-y-z is parsed as (x-y)-z, and not as x-(y-z) because of left-to-right associativity.

Precedence and associativity are independent from order of evaluation.

In C language, there is no left-to-right or right-to-left evaluation. Associativity is only for operators.

The expression sum() + difference() + remainder() is parsed as (sum() + difference()) + remainder() because of left-to-right associativity of + operator but any of the three functions can be evaluated first by the compiler.

Some basic rules around Operator Precedence

While the table above, holds every operator, but in general usage we mostly use arithmetic, logical and relational operators.

The arithmetic operators hold higher precedence than the logical and relational operators.

For example, if we have the following expression,

10 > 1 + 9;

This will return false because first the arithmetic operator + will be evaluated and then the comparison will be done. The above expression is treated as 10 > (1+9).

Hence, because 10 is not greater than 10, but it is equal, so the expression will return false.

Order of Precedence in Arithmetic Operators

There are many arithmetic operators in the C language, and the order of precedence is applicable for them too.

  1. ++ and -- (increment and decrement) operators hold the highest precedence.

  2. Then comes - (unary minus) operator

  3. Then comes *, / and % holding equal precedence.

  4. And at last, we have the + and - operators used for addition and subtraction, with the lowest precedence.

Operators on the same level of precedence are evaluated by the compiler from left to right (associativity, remember).

We can use parentheses to change the order of the evaluation.

Let's see an example,

#include <stdio.h>

int main() {
   // arithmetic operator precedence
   int a = 10, b = 20, c = 30, result;

   result = a * b + ++c;

   printf("The result is: %d", result);

   return 0;
}


The result is: 231

Run Code →

In the above code, first, ++c is evaluated because the increment operator has the highest precedence (value becomes 31), then a*b is evaluated because next in order of precedence is the multiplication operator (value becomes 200), then the + operator is evaluated (200 + 31), hence the result is 231.

Order of precedence in Relational/Logical Operators

In the case of the relational and logical operators, this order is followed:

  1. The ! (logical NOT) operator holds the highest precedence.

  2. Then comes > (greater than), >= (greater than or equal to), < (less than), and <= (less than or equal to) with the same precedence.

  3. Then we have the == and != operators.

  4. After that comes the && (logical AND) operator

  5. At last, we have the || (logical OR) operator with the lowest precedence.

For example, the expression !0 && 0 || 0, will give false as result.

Because first the NOT operator gets evaluated, which gives us 1, then 1 && 0 will give us 0, and then finally 0 || 0 will also give us 0, which is false.

Using Parentheses

We can control the order of execution by using parentheses in our expressions. Taking the last example, if we write the expression like this !(0 && 0) || 0, then the result will become true. Because then first the expression 0 && 0, which is inside the parentheses gets evaluated, then the NOT operator, and then the OR operator.

We can change the order of the arithmetic operators too, using the parentheses.

If there are multiple parentheses, then the innermost is executed first, then the second innermost and similarly we go on to the outer parentheses.

Conclusion:

In this tutorial, we learned the order of precedence of the different C language operators along with learning associativity and how using parentheses can change that.