Operator precedence determines the order in which operators are evaluated in an expression. When an expression contains multiple operators, C++ uses precedence rules to decide which operation is performed first.
Operator precedence defines the priority of operators in an expression.
int result = 10 + 5 * 2;
Multiplication has higher precedence than addition, so multiplication is performed first.
5 * 2 = 10
10 + 10 = 20
Therefore, result is 20.
Without understanding precedence, an expression containing several operators can produce a result different from what you expect.
int result = 20 - 5 * 2;
The multiplication is performed first:
5 * 2 = 10
20 - 10 = 10
So the result is 10, not 30.
Parentheses can be used to explicitly specify which operation should be performed first.
int result = (10 + 5) * 2;
First:
10 + 5 = 15
Then:
15 * 2 = 30
int result = 10 + 4 * 3;
Multiplication is performed before addition.
4 * 3 = 12
10 + 12 = 22
The result is 22.
int result = 20 + 10 / 2;
Division has higher precedence than addition.
10 / 2 = 5
20 + 5 = 25
The result is 25.
Multiplication and division have the same precedence. When operators have the same precedence, they are generally evaluated from left to right.
int result = 20 / 5 * 2;
Evaluation:
20 / 5 = 4
4 * 2 = 8
The result is 8.
Addition and subtraction have the same precedence and are generally evaluated from left to right.
int result = 20 - 5 + 3;
Evaluation:
20 - 5 = 15
15 + 3 = 18
When operators at the same precedence level are left-associative, the expression is evaluated from left to right.
int result = 100 / 5 * 2;
Evaluation:
100 / 5 = 20
20 * 2 = 40
Some operators are right-associative. Assignment operators are an important example.
int a, b, c;
a = b = c = 10;
The assignments are evaluated from right to left:
c = 10
b = c
a = b
All three variables receive the value 10.
The common arithmetic precedence order is:
int result = 10 + 20 * 3 % 4;
The multiplication and modulus operations have higher precedence than addition and are evaluated according to their associativity.
The modulus operator % has the same precedence level as multiplication and division.
int result = 10 + 17 % 5 * 2;
First, the operators at the higher precedence level are evaluated from left to right:
17 % 5 = 2
2 * 2 = 4
10 + 4 = 14
The result is 14.
Arithmetic operators generally have higher precedence than relational operators.
bool result = 10 + 5 > 12;
First:
10 + 5 = 15
Then:
15 > 12
The final result is true.
Equality operators such as == and != have lower precedence than relational operators such as > and <.
bool result = 10 < 20 == true;
The relational comparison is evaluated before the equality comparison.
The logical AND operator && has higher precedence than the logical OR operator ||.
bool result = true || false && false;
The AND operation is evaluated first:
false && false = false
true || false = true
Parentheses can make the intended logic clear.
bool result =
(age >= 18) && (hasID == true);
Using parentheses improves readability and reduces mistakes in complex expressions.
The assignment operator = has lower precedence than most arithmetic, relational, and logical operators.
int result;
result = 10 + 5 * 2;
The arithmetic expression is evaluated first, and the resulting value is then assigned to result.
The assignment operator = should not be confused with the equality operator ==.
int age = 20;
bool result = (age == 20);
The assignment happens when age is initialized, while age == 20 performs a comparison.
Unary operators operate on one operand. Examples include increment, decrement, logical NOT, unary plus, and unary minus.
int x = 5;
int a = -x;
bool b = !false;
Unary operators generally have higher precedence than many binary operators.
int x = 5;
int result = ++x * 2;
The pre-increment changes x to 6 before it participates in the multiplication.
6 * 2 = 12
The conditional operator ?: has lower precedence than many arithmetic, relational, and logical operators.
int age = 20;
std::string result =
age >= 18 ? "Adult" : "Minor";
The comparison age >= 18 is evaluated before the conditional operator selects the result.
int a = 10;
int b = 20;
int c = 5;
int result1 = a + b * c;
int result2 = (a + b) * c;
The two expressions produce different results because parentheses change the order of evaluation.
result1 = 110
result2 = 150
int marks1 = 70;
int marks2 = 80;
double average =
(marks1 + marks2) / 2.0;
The parentheses ensure that the two marks are added before division.
#include <iostream>
int main() {
int a, b, c;
std::cout << "Enter three numbers: ";
std::cin >> a >> b >> c;
int result = a + b * c;
std::cout << "Result: " << result;
return 0;
}
Multiplication is performed before addition in the expression.
For complex expressions, break the expression into smaller steps or use parentheses.
int result =
(10 + 5) * (20 - 15);
First:
10 + 5 = 15
20 - 15 = 5
Then:
15 * 5 = 75
Some commonly encountered precedence levels, from higher to lower, include:
This is a simplified learning order. C++ has a more detailed precedence table containing additional operators.
Precedence determines which operator has priority. Associativity determines the direction in which operators of the same precedence are grouped.
int result = 20 / 5 * 2;
Division and multiplication have the same precedence and are left-associative, so the expression is grouped as:
(20 / 5) * 2
#include <iostream>
int main() {
int a = 10;
int b = 20;
int c = 5;
int result =
a + b * c - 10 / 2;
std::cout << "Result: "
<< result;
return 0;
}
The multiplication and division operations are performed before addition and subtraction.
b * c = 100
10 / 2 = 5
10 + 100 - 5 = 105
Even when you know the precedence rules, parentheses can make code clearer.
Less explicit:
int result = a + b * c;
More explicit:
int result = a + (b * c);
Parentheses communicate the intended grouping directly to other programmers and to your future self.
| Category | Examples | General Precedence |
|---|---|---|
| Parentheses | () |
Very high |
| Unary operators | ++ -- ! - |
High |
| Multiplicative | * / % |
Higher than + and - |
| Additive | + - |
Medium |
| Relational | < > <= >= |
Lower than arithmetic |
| Equality | == != |
Lower than relational |
| Logical AND | && |
Lower than equality |
| Logical OR | || |
Lower than logical AND |
| Conditional | ?: |
Lower than logical OR |
| Assignment | = += -= *= /= |
Low |
Question: What is the result of the following expression?
int result = 10 + 5 * 2;