In C++, operator precedence determines the order in which operators are evaluated in a complex expression. Understanding this is important for writing (and thus interpreting) expressions correctly and avoiding unexpected errors.
Operators with higher precedence are evaluated before operators with lower precedence. If two operators have the same precedence, their evaluation is determined by the operator’s associativity *(left to right or right to left).
If you want to learn more about Operator Precedence
check the Introduction to Programming Course read more
Operator Precedence Table
Here is a table with the precedence of operators in C++, from highest to lowest. The highest precedence is evaluated first:
Priority | Operator | Description | Associativity |
---|---|---|---|
1 | () | Parentheses | N/A |
2 | ++ , -- | Increment and decrement (postfix) | Right to left |
3 | + , - | Unary positive and negative | Right to left |
4 | * , / , % | Multiplication, division, modulo | Left to right |
5 | + , - | Addition, subtraction | Left to right |
6 | << , >> | Bitwise shift | Left to right |
7 | < , > , <= , >= | Comparison | Left to right |
8 | == , != | Equality, inequality | Left to right |
9 | & | Bitwise AND | Left to right |
10 | ^ | Bitwise XOR | Left to right |
11 | ` | ` | Bitwise OR |
12 | && | Logical AND | Left to right |
13 | ` | ` | |
14 | ?: | Conditional operator (ternary) | Right to left |
15 | = , += , -= | Assignment and compound operators | Right to left |
How Operator Precedence Works
Expression Evaluation
Operator precedence determines the order in which operators are evaluated in an expression. For example, consider the following expression:
int result = 3 + 4 * 2; // equivalent to 3 + (4 * 2) = 11
Here, the multiplication operator (*
) has a higher precedence than the addition operator (+
). Therefore, 4 * 2
is evaluated first, resulting in 8
, and then 3
is added, yielding a result of 11
.
Use of Parentheses
To change the order of evaluation, you can use parentheses. Parentheses have the highest precedence and force the evaluation of the expression they contain first:
int result = (3 + 4) * 2;
In this case, 3 + 4
is evaluated first due to the parentheses, resulting in 7
, and then it is multiplied by 2
, yielding a result of 14
.
Associativity
When operators have the same precedence, associativity is applied to determine the order of evaluation. Most operators in C++ have left-to-right associativity.
int a = 1;
int b = 2;
int c = 3;
int result = a + b - c; // Evaluated as ((a + b) - c)
Here, both +
and -
have the same precedence and left-to-right associativity, so a + b
is evaluated first and then c
is subtracted.
However, some operators, such as assignment operators and the conditional ternary operator, have right-to-left associativity.
Complex Example
Consider the following expression:
int result = 5 + 2 * 3 - 4 / 2 + (1 + 3);
Let’s evaluate it step by step according to precedence and associativity:
- Evaluate the expression in parentheses:
(1 + 3)
, which results in4
. - Evaluate the multiplication:
2 * 3
, which results in6
. - Evaluate the division:
4 / 2
, which results in2
. - Evaluate the remaining operations from left to right:
5 + 6
results in11
.11 - 2
results in9
.9 + 4
results in13
.
Therefore, the final result of the expression is 13
.
Best Practices Tips
Although operator precedence allows omitting parentheses in many situations, it is good practice to use parentheses to improve code clarity. This not only makes the code easier to understand but also reduces the likelihood of errors.
int result = (5 + 2) * (3 - 1);