# Operator precedence and associativity

## Operator precedence and associativity

## Q) Explain operator presidency and associativity.

Operator Precedence of an operator:

- In an expression when there is more the one operator involved, the evaluation of them is done based on their priority.
- In C, every operator has its own priority (importance) this priority is known as the precedence of the operator.
- This operator having more precedence will be evaluated first and the remaining will be evaluated later. Consider the following expression:

a = 5 + 6 * 10 ;

- Here, the operation * will be performed first since it has higher precedence than +, later+operator will do its task and at the last =will be done.
- The priority of each operator in C is given in the following table.

**Associativity of operators**

- When an expression contains two or more operators of equal priority the tie between them is settled using the associativity of the operators.
- Associativity can be 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 the evaluation of any other sub-expression. Similarly, in cases 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 the same priority, that is between / and *. This tie is settled using the associativity of / and *. But both enjoy left to Right associativity.
- The figure shows for each operator which operand is unambiguous and which is not.

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

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

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

- Since both / and* have L to R associativity and only / has an unambiguous left operand (a necessary condition for L to R associativity) it is performed earlier.
- After that * will be operated.

### Operator Precedence Table

Description | Operator | Associativity |
---|---|---|

Function expression | ( ) | Left to Right |

Array Expression | [ ] | Left to Right |

Structure operator | – > | Left to Right |

Structure operator | . | Left to Right |

Unary minus | – | Right to Left |

Increment / Decrement | ++ — | Right to Left |

One’s compliment | ~ | Right to Left |

Negation | ! | Right to Left |

Address of | & | Right to Left |

Value of address | * | Right to Left |

Typecast | ( type ) | Right to Left |

Size in bytes | sizeof | Right to Left |

Multiplication | * | Left to Right |

Division | / | Left to Right |

Modulus | % | Left to Right |

Addition | + | Left to Right |

Subtraction | – | Left to Right |

Left Shift | << | Left to Right |

Right Shift | >> | Left to Right |

Less than | < | Left to Right |

Less than or equal to | <= | Left to Right |

Greater than | > | Left to Right |

Greater than or equal to | >= | Left to Right |

Equal to | == | Left to Right |

Not equal to | != | Left to Right |

Bitwise AND | & | Left to Right |

Bitwise exclusive OR | ^ | Left to Right |

Bitwise inclusive OR | | | Left to Right |

Logical AND | && | Left to Right |

Logical OR | || | Left to Right |

Conditional | ? : | Right to Left |

Assignment | = | Right to Left |

*= /= %= | Right to Left | |

+= -= &= | Right to Left | |

^= |= | Right to Left | |

<<= >>= | Right to Left | |

Comma | , | Right to Left |