C Operator Precedence โ Complete Table with Examples
Introduction โ What Is Operator Precedence in C?
Operator precedence in C defines the order in which operators are evaluated in complex expressions. When multiple operators appear in a single expression, precedence determines which operations are performed firstโjust like the order of operations in mathematics (e.g., multiplication before addition).
In this guide, youโll learn:
- What operator precedence and associativity mean
- Complete table of C operator precedence
- Examples where precedence affects result
- Best practices using parentheses for clarity
What Is Precedence and Associativity?
- Precedence determines which operator is evaluated first.
- Associativity determines the direction of evaluation (left-to-right or right-to-left) when operators have the same precedence.
Example:
int result = 5 + 2 * 3; // result = 11 (not 21)
Here, * has higher precedence than +.
C Operator Precedence Table (Highest to Lowest)
| Precedence | Operators | Associativity | Description |
|---|---|---|---|
| 1 | () [] -> . | Left to Right | Function call, array, struct access |
| 2 | ++ -- + - ! ~ * & sizeof | Right to Left | Unary, address-of, dereference |
| 3 | * / % | Left to Right | Multiplication, division, modulus |
| 4 | + - | Left to Right | Addition, subtraction |
| 5 | << >> | Left to Right | Bitwise shift left, right |
| 6 | < <= > >= | Left to Right | Relational operators |
| 7 | == != | Left to Right | Equality/inequality |
| 8 | & | Left to Right | Bitwise AND |
| 9 | ^ | Left to Right | Bitwise XOR |
| 10 | ` | ` | Left to Right |
| 11 | && | Left to Right | Logical AND |
| 12 | ` | ` | |
| 13 | ?: | Right to Left | Ternary conditional |
| 14 | = += -= *= /= etc. | Right to Left | Assignment |
| 15 | , | Left to Right | Comma (sequence) |
Examples of Operator Precedence
Without Parentheses
int a = 10, b = 5, c = 2;
int result = a + b * c; // b * c = 10, then 10 + 10 = 20
With Parentheses
int result = (a + b) * c; // a + b = 15, then 15 * 2 = 30
Associativity Example
int a = 5;
int result = a = a + 2 * 3; // 2 * 3 = 6, then a + 6 = 11, then a = 11
In a = a + 2 * 3;, the * is evaluated first, then +, then =.
Best Practices
- Always use parentheses to clarify the intended order.
- Avoid writing complex one-liners with multiple operators.
- Use indentation and line breaks for readability when needed.
- Donโt assume operator precedenceโlook it up or add parentheses.
Summary โ Recap & Next Steps
Operator precedence and associativity in C are vital for writing correct and predictable expressions. Misunderstanding precedence can lead to logical errors and unexpected results.
Key Takeaways:
- Precedence defines which operators are evaluated first.
- Associativity defines direction (left-to-right or right-to-left).
- Use parentheses to control or override the default precedence.
- Multiplication and division have higher precedence than addition/subtraction.
Real-World Relevance:
Operator precedence is crucial in expression-heavy code, such as mathematical computations, conditional logic, algorithm design, and embedded C programming where performance and correctness matter.
Frequently Asked Questions (FAQ)
What is operator precedence?
It determines the priority of operators in expressions without parentheses. Operators with higher precedence are evaluated before lower ones.
What is operator associativity?
When multiple operators with the same precedence appear, associativity defines the evaluation directionโusually left-to-right.
How do I ensure correct evaluation?
Use parentheses () to explicitly define the order of operations:
int result = (a + b) * c;
Does && have higher precedence than ||?
Yes. Logical AND (&&) has higher precedence than Logical OR (||).
Can I rely on operator precedence across compilers?
Yes. C operator precedence is defined by the language standard and behaves the same across standard-compliant compilers.
Share Now :
