C++ Operator Precedence
In this page:
result = a + b * c; // * / % run before + -
result = (a + b) * c; // parentheses run first
Arithmetic Operator Precedence
C++ applies the same order-of-operations rules taught in basic math: multiplication and division are evaluated before addition and subtraction, so 2 + 3 * 4 evaluates to 14, not 20.
Misjudging precedence is a common source of subtly wrong calculations that compile without any error.
Example: Arithmetic Operator Precedence
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare result and set it to 2 + 3 * 4
int result = 2 + 3 * 4;
// Print to the console with cout
std::cout << result << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Using Parentheses to Force Precedence
Wrapping part of an expression in parentheses forces it to be evaluated first, overriding the default precedence rules entirely — (2 + 3) * 4 evaluates to 20 precisely because the addition is now grouped explicitly.
When in doubt about how an expression will be evaluated, adding parentheses costs nothing and removes all ambiguity for both the compiler and future readers.
Example: Using Parentheses to Force Precedence
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare result and set it to (2 + 3) * 4
int result = (2 + 3) * 4;
// Print to the console with cout
std::cout << result << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Relational and Logical Precedence
Relational operators like > and < bind more tightly than logical operators like && and ||, which is why a > 5 && b < 10 is parsed as (a > 5) && (b < 10) without needing explicit parentheses around each comparison.
Relying on this default ordering without parentheses is fine for simple conditions, but larger expressions benefit from explicit grouping for clarity.
Example: Relational and Logical Precedence
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int a = 6, b = 5;
// Declare result and set it to a > 5 && b < 10
bool result = a > 5 && b < 10;
// Print to the console with cout
std::cout << result << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Left-to-Right Associativity
When two operators share the same precedence level, such as a - b + c, C++ evaluates them left to right, treating it as (a - b) + c.
This left-to-right rule is what most arithmetic and comparison operators follow, and it matches the way you'd naturally read the expression.
Example: Left-to-Right Associativity
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int a = 10, b = 3, c = 2;
// Declare result and set it to a - b + c
int result = a - b + c;
// Print to the console with cout
std::cout << result << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Right-to-Left Associativity of Assignment
Assignment is an exception to left-to-right evaluation — it associates right to left, which is exactly what makes chained assignment like a = b = c = 0; work: c = 0 happens first, then that result is assigned to b, and finally to a.
This lets you initialize several variables to the same value in one compact line.
Example: Right-to-Left Associativity of Assignment
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int a, b, c;
a = b = c = 5;
// Print to the console with cout
std::cout << a << " " << b << " " << c << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Assuming
a + b * cevaluates left to right, when multiplication happens before addition. - Writing
a & b == c, where==binds tighter than&and gives a surprising result. - Not using parentheses to make intent clear, so readers and the compiler group operations differently than expected.
- Arithmetic, relational, logical, bitwise, and assignment operators perform calculations and comparisons.
- Increment and decrement operators change a value by one, and the ternary operator is a short conditional.
- Operator precedence determines the order in which expressions are evaluated.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: