You are on page 1of 2

Q.15:- Explain Operator precedence and associativity with chart.

**Operator Precedence and Associativity**:

Operator precedence defines the order in which operators are evaluated in an expression,
while associativity determines the direction in which operators are grouped when they
have the same precedence level.

**Operator Precedence Chart**:

Highest Precedence:

1. () - Parentheses

2. [] - Array subscript

3. -> - Member access through pointer

4. . - Member access

5. ++, -- - Postfix increment and decrement

6. +, - - Unary plus and minus

7. !, ~ - Logical NOT, Bitwise NOT

8. *, /, % - Multiplication, Division, Modulus

9. +, - - Addition, Subtraction

10. <<, >> - Bitwise left shift, Bitwise right shift

11. <, <=, >, >= - Relational operators

12. ==, != - Equality operators

13. & - Bitwise AND

14. ^ - Bitwise XOR

15. | - Bitwise OR

16. && - Logical AND

17. || - Logical OR

18. ?: - Ternary conditional


19. = - Assignment

20. +=, -=, *=, /= - Compound assignment

Lowest Precedence:

**Associativity**:

- **Left Associativity**: Operators with left associativity are grouped from left to right. For
example, the `+` operator has left associativity, so `a + b + c` is evaluated as `(a + b) + c`.

- **Right Associativity**: Operators with right associativity are grouped from right to left.
For example, the `=` operator has right associativity, so `a = b = c` is evaluated as `a = (b =
c)`.

Understanding operator precedence and associativity is essential for writing expressions


that produce the expected results. It helps clarify the order of operations and grouping of
operands in complex expressions.

You might also like