Explain precedence of operators in c programming
Explain precedence of operators in c programming
current context (Tuesday, April 15, 2025, 11:44 AM IST, Bhubaneswar, Odisha, India). Operator
precedence dictates the order in which different operators are evaluated within a single
expression. It's crucial for understanding how C interprets and calculates the result of complex
expressions.
Think of it like the order of operations you learned in mathematics (PEMDAS/BODMAS). In C,
different operators have different levels of "priority." Operators with higher precedence are
evaluated before operators with lower precedence.
Here's a table summarizing the precedence of common C operators, from highest to lowest.
Operators on the same row have the same precedence and their associativity determines the
evaluation order.
Precedence Operator(s) Associativity
1 () (Function call, Parentheses), Left-to-right
[] (Array subscript), . (Member
access), -> (Pointer to member
access)
2 ! (Logical NOT), ~ (Bitwise Right-to-left
NOT), ++ (Prefix increment), --
(Prefix decrement), + (Unary
plus), - (Unary minus), *
(Dereference), & (Address-of),
sizeof, _Alignof (C11), _Generic
(C11)
3 * (Multiplication), / (Division), % Left-to-right
(Modulo)
4 + (Addition), - (Subtraction) Left-to-right
5 << (Left shift), >> (Right shift) Left-to-right
6 < (Less than), <= (Less than or Left-to-right
equal to), > (Greater than), >=
(Greater than or equal to)
7 == (Equal to), != (Not equal to) Left-to-right
8 & (Bitwise AND) Left-to-right
9 ^ (Bitwise XOR) Left-to-right
10 ` ` (Bitwise OR)
11 && (Logical AND) Left-to-right
12 `
13 ? : (Conditional operator) Right-to-left
14 = (Assignment), +=, -=, *=, /=, =`
%=, <<=, >>=, &=, ^=, `
15 , (Comma operator) Left-to-right
Explanation of the Table:
● Higher Precedence (Top of the Table): Operators at the top of the table have higher
precedence and are evaluated first.
● Lower Precedence (Bottom of the Table): Operators at the bottom have lower
precedence and are evaluated later.
● Associativity: When operators have the same precedence, associativity determines the
order of evaluation:
○ Left-to-right: Operators are evaluated from left to right (e.g., a - b + c is evaluated
as (a - b) + c).
○ Right-to-left: Operators are evaluated from right to left (e.g., a = b = 5 is evaluated
as a = (b = 5)).
Examples to Illustrate Precedence:
1. int result = 2 + 3 * 4;
○ Multiplication (*) has higher precedence than addition (+).
○ So, 3 * 4 is evaluated first, resulting in 12.
○ Then, 2 + 12 is evaluated, and result becomes 14.
2. int value = (5 - 2) * 3;
○ Parentheses () have the highest precedence.
○ So, (5 - 2) is evaluated first, resulting in 3.
○ Then, 3 * 3 is evaluated, and value becomes 9.
3. int flag = 1 && 0 || 1;
○ Logical AND (&&) has higher precedence than logical OR (||).
○ So, 1 && 0 is evaluated first, resulting in 0 (false).
○ Then, 0 || 1 is evaluated, and flag becomes 1 (true).
4. int x = 10; int y = ++x * 2;
○ Prefix increment (++x) has higher precedence than multiplication (*).
○ First, x is incremented to 11.
○ Then, 11 * 2 is evaluated, and y becomes 22.
5. int a = 5; int b = a++; int c = ++a;
○ Postfix increment (a++) has lower precedence than prefix increment (++a) in terms
of when the increment happens relative to the rest of the expression.
○ In int b = a++;: b is assigned the current value of a (which is 5), and then a is
incremented to 6. So, b becomes 5.
○ In int c = ++a;: a is first incremented to 7, and then this new value (7) is assigned to
c. So, c becomes 7.
Importance of Understanding Precedence:
● Predictable Evaluation: Knowing the precedence rules ensures that expressions are
evaluated in the intended order, leading to correct program behavior.
● Avoiding Errors: Misunderstanding precedence can lead to unexpected results and
logical errors in your code.
● Code Readability: While the rules exist, using parentheses to explicitly group operations
can significantly improve the readability of complex expressions, even if the default
precedence would yield the desired result. It makes your code easier to understand and
maintain.
Best Practice: Use Parentheses for Clarity:
Even though C has well-defined precedence rules, it's often a good practice to use parentheses
to make the order of evaluation explicit, especially in complex expressions. This reduces
ambiguity and makes your code easier to read and understand, both for yourself and for others
who might work with your code here in Bhubaneswar or elsewhere. For instance, instead of
relying on the precedence of && over ||, you might write (condition1 && condition2) || condition3
to clearly indicate your intent.