Operator Precedence and Associativity in C

Operator precedence: It dictates which operator first get applied in an expression.

Associativity: It defines the order in which operators of the same precedence are evaluated i.e either from right to left or left to right.

Consider the following example:

24 + 5 * 4

Here we have two operators + and *, Which operation do you think will be evaluated first, addition or multiplication ? If the addition is applied first then answer will be 116 and if the multiplication is applied first answer will be 44. To answer such question we need to consult the operator precedence table.

In C, each operator has a fixed priority or precedence in relation to other operators. As a result operator with a higher precedence is evaluated before the operator of a lower precedence. Operators that appear in the same group have the same precedence. The following table lists operator precedence and associativity.

operator-precedence-and-associativity

Operators in the top have higher precedence and it decreases as we move towards the bottom.

From the table we can conclude that is above +, so * operator has higher precedence than +, therefore 5 * 4 will be evaluated first.

operator-precedence-example-1

Here are some more examples:

Example 1:

34 + 12/4 - 45

operator-precedence-example-2

Here / has higher precedence hence 12/4 is evaluated first. The operators + and - have the same precedence because they are in the same group, so which one of them will be evaluated first ? To solve this problem you need to consult the associativity of the operator. As you can see in table, the operators + and - have same precedence and associates from left to right therefore in our expression 34 + 12/4 - 45 after division, addition(+) will be performed before subtraction (-) .

Example 2:

12 + 3 - 4 / 2 < 3 + 1

operator-precedence-example-3

Here / has higher precedence hence 12/4 is evaluated first. Then + and - operators have same precedence and associates from left to right, therefore in our expression 12 + 3 - 4 / 2 < 3 + 1 after division, + will be evaluated before - .From the precedence table you can see that precedence of < operator is lower than that of /, + and - hence it will be evaluated at last.

12 + 3 - 4 / 2 < 3 + 1 after division, + will be evaluated before -. From the precedence table you can see that precedence of < operator is lower than that of /, + and -. Hence it will be evaluated at last.

Using Parentheses #

You can use parentheses to change the order of precedence, as it has the highest precedence. (see the precedence table). For example:

3 + 4 * 2

Here * will be evaluated first followed by +, but you can change this behavior by using parentheses.

(3 + 4) * 2

Now + will be evaluated first followed by *.

You can also nest parentheses like this:

(2+(3+2)) * 10

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

In the next chapter, we will learn about the if else statement in C.