While executing an arithmetic statement, which has two or more operators, we may have some problems as to how exactly does it get executed. For example, does the expression 2 * x – 3 * y correspond to (2x)-(3y) or to 2(x-3y)? Similarly, does A / B * C correspond to A / (B * C) or to (A / B) * C? To answer these questions satisfactorily one has to understand the ‘hierarchy’ of operations. The priority or precedence in which the operations in an arithmetic statement are performed is called the hierarchy of operations. The hierarchy of commonly used operators is shown in table

Priority | Operators | Description |
---|---|---|

1^{st} | * / % | multiplication, division, modular division |

2^{nd} | + – | addition, subtraction |

3^{rd} | = | assignment |

**Hierarchy of Operations in C**

**A few examples would clarify the issue further**

i = 2 * 3 / 4 + 4 / 4 + 8 – 2 + 5 / 8

Stepwise evaluation of this expression is shown below:

i = 2 * 3 / 4 + 4 / 4 + 8 – 2 + 5 / 8

i = 6 / 4 + 4 / 4 + 8 – 2 + 5 / 8 operation: *

i = 1 + 4 / 4 + 8 – 2 + 5 / 8 operation: /

i = 1 + 1+ 8 – 2 + 5 / 8 operation: /

i = 1 + 1 + 8 – 2 + 0 operation: /

i = 2 + 8 – 2 + 0 operation: +

i = 10 – 2 + 0 operation: +

i = 8 + 0 operation : –

i = 8 operation: +

**Associativity of Operators**

When an expression contains two operators of equal priority the tie between them is settled using the associativity of the operators. Associativity can be of two types—Left to Right or Right to Left. Left to Right associativity means that the left operand must be

unambiguous. Unambiguous in what sense? It must not be involved in evaluation of any other sub-expression. Similarly, in case of Right to Left associativity the right operand must be unambiguous. Let us understand this with an example.

Consider the expression

a = 3 / 2 * 5 ;

Here there is a tie between operators of same priority, that is between / and *. This tie is settled using the associativity of / and *. But both enjoy Left to Right associativity. Figure 1.10 shows for each operator which operand is unambiguous and which is not.

Operator | Left | Right | Remark |
---|---|---|---|

/ | 3 | 2 or 2 * 5 | Left operand is unambiguous, Right is not |

* | 3 / 2 or 2 | 5 | Right operand is unambiguous, Left is not |

**Associativity of Operators**

Since both / and * have L to R associativity and only / has unambiguous left operand (necessary condition for L to R associativity) it is performed earlier.