100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Operator Precedence and Associativity in C++

Understand how C++ operator precedence and associativity determine evaluation order in complex expressions, with common gotchas explained.

OperatorsIntermediate7 min readJul 7, 2026
Analogies

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

cpp
result = a + b * c;      // * has higher precedence than +
result = a = b = c;      // = is right-associative
result = a - b - c;      // - is left-associative: (a - b) - c

3. 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

cpp
#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

text
a + b * c = 14
a < b && b < c = true
x = 10, y = 10

6. 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

Was this page helpful?

Topics covered

#CStudyNotes#Programming#OperatorPrecedenceAndAssociativityInC#Operator#Precedence#Associativity#Syntax#StudyNotes#SkillVeris#ExamPrep