Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is C Operator Precedence and Associativity?
In C programming, operator precedence and associativity determine the order in which operators are evaluated in complex expressions. Understanding these concepts is crucial for writing correct C programs and predicting expression results.
Operator Precedence
Operator precedence defines the priority order in which operators are evaluated in an expression. When multiple operators exist in an expression, the operator with higher precedence is evaluated first. For example, multiplication has higher precedence than addition.
Syntax
expression = operand1 operator1 operand2 operator2 operand3 /* Evaluation order depends on operator precedence and associativity */
Operator Associativity
Operator associativity determines the evaluation order when multiple operators with the same precedence appear in an expression. It can be either left-to-right or right-to-left.
Precedence and Associativity Table
| Precedence | Operator | Description | Associativity |
|---|---|---|---|
| 1 (Highest) | () [] -> . | Function call, array subscript, structure member access | Left to Right |
| 2 | ! ~ + - ++ -- & * sizeof | Unary operators | Right to Left |
| 3 | * / % | Multiplicative operators | Left to Right |
| 4 | + - | Additive operators | Left to Right |
| 5 | << >> | Shift operators | Left to Right |
| 6 | < <= > >= | Relational operators | Left to Right |
| 7 | == != | Equality operators | Left to Right |
| 8 | & | Bitwise AND | Left to Right |
| 9 | ^ | Bitwise XOR | Left to Right |
| 10 | | | Bitwise OR | Left to Right |
| 11 | && | Logical AND | Left to Right |
| 12 | || | Logical OR | Left to Right |
| 13 | ?: | Conditional operator | Right to Left |
| 14 | = += -= *= /= %= &= ^= |= <<= >>= | Assignment operators | Right to Left |
| 15 (Lowest) | , | Comma operator | Left to Right |
Example 1: Operator Precedence
This example demonstrates how precedence affects expression evaluation −
#include <stdio.h>
int main() {
int a = 10, b = 20, c = 30;
int result1 = a + b * c; /* * has higher precedence than + */
int result2 = (a + b) * c; /* parentheses override precedence */
printf("a + b * c = %d + %d * %d = %d<br>", a, b, c, result1);
printf("(a + b) * c = (%d + %d) * %d = %d<br>", a, b, c, result2);
return 0;
}
a + b * c = 10 + 20 * 30 = 610 (a + b) * c = (10 + 20) * 30 = 900
Example 2: Operator Associativity
This example shows how associativity works with operators of equal precedence −
#include <stdio.h>
int main() {
int a = 100, b = 10, c = 5;
/* Division and multiplication have same precedence, left-to-right associativity */
int result1 = a / b * c; /* Evaluated as (a / b) * c */
int result2 = a / (b * c); /* Explicit parentheses change order */
printf("a / b * c = %d / %d * %d = %d<br>", a, b, c, result1);
printf("a / (b * c) = %d / (%d * %d) = %d<br>", a, b, c, result2);
/* Assignment has right-to-left associativity */
int x, y, z;
x = y = z = 5; /* Evaluated as x = (y = (z = 5)) */
printf("After x = y = z = 5: x=%d, y=%d, z=%d<br>", x, y, z);
return 0;
}
a / b * c = 100 / 10 * 5 = 50 a / (b * c) = 100 / (10 * 5) = 2 After x = y = z = 5: x=5, y=5, z=5
Key Points
- Higher precedence operators are evaluated before lower precedence ones
- Parentheses can override natural precedence and associativity
- Most operators have left-to-right associativity except unary, assignment, and conditional operators
- Understanding precedence prevents logical errors in complex expressions
Conclusion
Operator precedence and associativity are fundamental concepts in C that control expression evaluation order. Mastering these rules helps write clearer code and avoid common programming mistakes.
