Menu Close

C Operator Precedence and Associativity

Posted in C Programming

Operator precedence and associativity in the C programming language determine the order in which operators are evaluated in an expression. Understanding these concepts is crucial for writing correct and predictable code. Here is a detailed explanation of operator precedence and associativity in C:

1.Operator Precedence

Operator precedence determines the priority of operators in an expression. Operators with higher precedence are evaluated before operators with lower precedence. If operators have the same precedence, the associativity rules (explained below) come into play.

The following is a list of C operators, arranged in descending order of precedence:

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

For example, in the expression a + b * c, the multiplication operator (*) has higher precedence than the addition operator (+), so b * c is evaluated first, and then the result is added to a.

Remember that this list represents the relative precedence of operators in C. Operators with higher precedence are evaluated before operators with lower precedence. Parentheses can be used to explicitly group expressions and override the default precedence.

2. Operator Associativity

Associativity determines the order in which operators with the same precedence are evaluated.

There are two types of associativity:

  • Left-to-right associativity: Operators with left-to-right associativity are evaluated from left to right. Most operators in C, such as addition (+), subtraction (-), and assignment (=), have left-to-right associativity. For example, in the expression a - b - c, the subtraction operator (-) has left-to-right associativity, so a - b is evaluated first, and then the result is subtracted from c.
  • Right-to-left associativity: Operators with right-to-left associativity are evaluated from right to left. The unary operators, such as the unary minus (-) and the postfix increment and decrement operators (++ and –), have right-to-left associativity. For example, in the expression --a - b, the decrement operator (–) has right-to-left associativity, so a is decremented first, and then the result is subtracted from b.

It’s important to note that parentheses can be used to override the default precedence and associativity rules. Expressions enclosed in parentheses are evaluated first.

Related:   The Semicolon (;) is Used to Terminate Statements

Understanding operator precedence and associativity is essential for writing clear and unambiguous code, especially when expressions involve multiple operators. By adhering to these rules, programmers can ensure that their code behaves as intended and produces accurate results.

 Example 2.1. C Operator Precedence and Associativity

 



main()
{
   int a = 20;
   int b = 10;
   int c = 15;
   int d = 5;
   int e;

   e = (a + b) * c / d; // ( 30 * 15 ) / 5
   printf("(a + b) * c / d results %d", e );

   e = ((a + b) * c) / d; // (30 * 15 ) / 5
   printf("((a + b) * c) / d results %d" , e );

   e = (a + b) * (c / d); // (30) * (15/5)
   printf("(a + b) * (c / d) results %d", e );

   e = a + (b * c) / d; // 20 + (150/5)
   printf("a + (b * c) / d results %d" , e );

   return 0;
}

Results:

(a + b) * c / d results 90
((a + b) * c) / d results 90
(a + b) * (c / d) results 90
a + (b * c) / d results 50

Leave a Reply