๐งฉ 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 :