CodingBison

When a C expression has multiple operations (it happens more often than not!), then it is possible for some of these operations to be ambiguous. As an example, for expression, "var_num = 6 * 2 + 3", it is not clear if we multiply 6 by 2 first and then add 3 (so that var_num becomes 15) or add 2 to 3 first and then multiply with 6 (so that var_num becomes 30).

To handle such ambiguous cases, C maintains a precedence order for various operators.

We list the precedence order in the table provided below. The list contains some of the common operators and hence, is not exhaustive. We list these operators from top to bottom, in descending precedence -- thus, operators at the top have higher precedence that those that are below it. Also, operators present in the same row have equal precedence.


Table: C Operator Precedence
OperatorDescription
(), [], -->, .Parentheses, Array Index, Pointer member, data structure member
/, %, *,Division, Modulo, and Multiplication
+, -Addition and Subtraction
<<, >>Shift Operators (Left-shift and right-shift)
<, <=, <, >=Comparison Operators
==, !=Equality and Inequality
x1 & x2Bitwise AND
x1 ^ x2Bitwise XOR
x1 | x2Bitwise OR
!xLogical Negation
x1 && x2Logical AND
x1 || x2Logical OR
?:Conditional Ternary Operator
=, +=, -=, /=, *=, %=, &=, ^=, !=, <<=, >>=Binary Operators

Thus, if an expression has both "*" and "+", then C uses the precedence list to conclude that "*" has a higher precedence than "+". Accordingly, the compiler would execute "*" followed by "+".

Now that we have seen the operator precedence, let us revisit our earlier expression of "var_num = 6 * 2 + 3"! As per the precedence list, "*" has precedence over "+". So, C first multiples 6 and 2 (which yields 12) and then adds 3 to the result. Thus, the value of var_num becomes 15 and not 30.

To help avoid ambiguity, the right thing to do is to use parenthesis. As we can see in the above list, parentheses has a higher precedence than other operators and hence, where ever there is less clarity, we can simply use parentheses to avoid ambiguity. Using parentheses is a good programming practice. As a bonus, it also improves code-readability!

On the other hand, if we wish to do addition first, followed by multiplication, then we can specify that as "var_num = 6 * (2 + 3)". Else, if we intend to multiply 6 and 2 first and then add 3 to the result, then we can specify it as "var_num = (6 * 2) + 3".

Lastly, some of the rows in the table have multiple operators. As we noted earlier, for such rows, the precedence is same for all of them. So, a natural question would be what happens if we have both of these operators together in the same expression. This is okay since the nature of these operators allow us to carry them out in either order. Thus, if we have an expression "10 + 5 - 1", then since both "+" and "-" have the same precedence, we can either add 10 and 5 first followed by subtracting 1 or we can subtract 1 from 5 first followed by adding 10. The result would be same.





comments powered by Disqus