C language content

In C language, operator precedence and associativity are rules that dictate the order in which operators are evaluated in expressions. Understanding these rules is crucial for writing correct and efficient code. Here’s an in-depth explanation:

Operator Precedence

Operator precedence determines the order in which different operators are applied in an expression. Operators with higher precedence are evaluated before operators with lower precedence. For instance, in the expression a + b * c, the multiplication operator (*) has higher precedence than the addition operator (+), so b * c is evaluated before a +.

Precedence Table

Here is a list of operators sorted by their precedence (from highest to lowest):

  1. Primary Expressions:
    • (), [], ->, .
  2. Unary Operators:
    • +, -, !, ~, ++, --, &, *, sizeof
  3. Multiplicative Operators:
    • *, /, %
  4. Additive Operators:
    • +, -
  5. Shift Operators:
    • <<, >>
  6. Relational Operators:
    • <, <=, >, >=
  7. Equality Operators:
    • ==, !=
  8. Bitwise AND:
    • &
  9. Bitwise XOR:
    • ^
  10. Bitwise OR:
    • |
  11. Logical AND:
    • &&
  12. Logical OR:
    • ||
  13. Conditional Operator:
    • ?:
  14. Assignment Operators:
    • =, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=
  15. Comma Operator:
    • ,

Operator Associativity

Operator associativity determines the order in which operators of the same precedence level are evaluated. Associativity can be either left-to-right or right-to-left.

Associativity Rules

  1. Left-to-Right Associativity:

    • Most binary operators (e.g., +, -, *, /, ==, !=, &, |, ^, &&, ||, etc.)
    • Example: a - b + c is evaluated as (a - b) + c.
  2. Right-to-Left Associativity:

    • Unary operators (e.g., !, ~, ++, --, &, *, +, -, sizeof)
    • Assignment operators (e.g., =, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=)
    • Conditional operator (?:)
    • Example: a = b = c is evaluated as a = (b = c).

Practical Examples

1. Unary Operators:

				
					int x = 5;
int y = ++x;  // y = 6, x = 6 (prefix increment)
int z = x--;  // z = 6, x = 5 (postfix decrement)

				
			

2. Binary Operators:

				
					int a = 10;
int b = 5;
int c = a + b * 2;  // c = 10 + (5 * 2) = 20

				
			

3. Associativity:

				
					int a = 3, b = 4, c = 5;
int result = a - b - c;  // result = (a - b) - c = (3 - 4) - 5 = -6 (left-to-right associativity)

				
			

4. Complex Expression:

				
					int x = 2, y = 3, z = 4;
int result = x + y * z / y - x;  // result = 2 + ((3 * 4) / 3) - 2 = 2 + 4 - 2 = 4

				
			

Summary

  • Operator Precedence: Determines the order in which operators are evaluated.
  • Operator Associativity: Determines the order in which operators of the same precedence level are evaluated.
  • Left-to-Right Associativity: Most binary operators.
  • Right-to-Left Associativity: Unary operators, assignment operators, and the conditional operator.

Understanding these rules helps in avoiding bugs and writing expressions that produce the intended results. When in doubt, parentheses can be used to explicitly define the order of evaluation.

Scroll to Top