1. Intro
When an expression contains multiple operators, C++ needs rules to decide which operator applies first. **Precedence** determines which operator binds tighter (is evaluated first) when different operators appear together. **Associativity** determines the evaluation order when operators of the *same* precedence appear together — either left-to-right or right-to-left.
Cricket analogy: In a run-rate calculation, multiplication of overs by run-rate happens before addition of extras — precedence — while tallying several consecutive boundaries of equal weight is just processed left to right, associativity, the same two rules C++ uses to evaluate expressions.
2. Syntax
result = a + b * c; // * has higher precedence than +
result = a = b = c; // = is right-associative
result = a - b - c; // - is left-associative: (a - b) - c3. Explanation
From highest to lowest precedence (a simplified, high-level view): unary operators (!, unary -, ++, --) bind tightest; then multiplicative operators (* / %); then additive operators (+ -); then relational/comparison operators (< > <= >=); then equality operators (== !=); then logical AND (&&); then logical OR (||); then the assignment operators (= += -= etc.); with the comma operator (,) binding loosest. Most binary operators (like +, -, *, /) are left-associative, meaning a - b - c is (a - b) - c. Assignment operators are right-associative, which is why a = b = c works as a = (b = c).
Cricket analogy: Just as DRS checks run-out before no-ball, in fixed priority order, C++ evaluates unary operators, then * / %, then + -, down to assignment last; and just as boundary runs are tallied left to right, a - b - c equals (a-b)-c, while a = b = c flows right to left.
A classic precedence gotcha: a = b == c; parses as a = (b == c) because == binds tighter than =. If you actually wanted to assign c to both a and b, you need a = (b = c); explicitly, or better, avoid the ambiguity by writing two separate statements.
4. Example
#include <iostream>
using namespace std;
int main() {
int a = 2, b = 3, c = 4;
int result1 = a + b * c; // multiplication first
cout << "a + b * c = " << result1 << endl;
bool result2 = a < b && b < c; // relational before logical AND
cout << boolalpha << "a < b && b < c = " << result2 << endl;
int x, y;
x = y = 10; // right-associative assignment
cout << "x = " << x << ", y = " << y << endl;
return 0;
}5. Output
a + b * c = 14
a < b && b < c = true
x = 10, y = 106. Key Takeaways
- Precedence order (high to low, simplified): unary operators, then
* / %, then+ -, then relational (< > <= >=), then equality (== !=), then&&, then||, then assignment operators, then comma. * / %bind tighter than+ -, which bind tighter than relational/comparison operators.- Relational/equality operators bind tighter than
&&, which binds tighter than||. - Most binary operators are left-associative; assignment operators are right-associative, enabling chains like
a = b = c;.
Practice what you learned
1. What is the value of `2 + 3 * 4` in C++?
2. How does `a - b - c` associate given that `-` is left-associative?
3. Which has higher precedence: `&&` or `==`?
4. Why does `x = y = 10;` correctly assign 10 to both x and y?
5. In `a < b && b < c`, which operators evaluate first?
Was this page helpful?
You May Also Like
Arithmetic Operators in C++
Learn how C++ arithmetic operators (+, -, *, /, %) work, including integer division truncation and modulo rules, with runnable examples.
Logical Operators in C++
Master the C++ logical operators &&, ||, and !, and understand short-circuit evaluation and how it can skip side effects.
Assignment Operators in C++
Learn C++ assignment operators including =, +=, -=, *=, /=, and %=, and how compound assignment shortens common update expressions.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics