In C++, the operator precedence determines the order of execution for different operators in an expression, with higher precedence operators being executed first; if the precedence is the same, the order of execution is determined by associativity (left-to-right or right-to-left). This concept is also a common point of examination and difficulty in GESP, and students must master it thoroughly.
1. Overall Precedence Table (High → Low)

2. Core Rules and Examples
1. Core of Precedence
- Parentheses > Unary Operators > Arithmetic Operators > Relational Operators > Bitwise Operators > Logical Operators > Assignment Operators
- Specific Ranking:
- Parentheses > Unary Operators (++, –, !, &, *) > Arithmetic (Multiplication/Division > Addition/Subtraction) > Shift > Relational > Equality > Bitwise (>, ^, |) > Logical (&&, ||) > Ternary > Assignment > Comma.
- Best Practices:
- If the complexity of the expression is high, use parentheses to clearly define the order of execution, avoiding reliance on memory of precedence (readability is far superior to rote memorization of precedence).
2. Key Examples of Associativity
- Left Associativity (same level from left to right):
<span><span>a / b * c</span></span>→<span><span>(a / b) * c</span></span>(arithmetic operators);<span><span>a && b && c</span></span>→<span><span>(a && b) && c</span></span>(logical AND). - Right Associativity (same level from right to left):
<span><span>a = b = c</span></span>→<span><span>a = (b = c)</span></span>(assignment operator);<span><span>a = !b + c</span></span>→<span><span>a = ((!b) + c)</span></span>(unary operator takes precedence over addition);<span><span>a ? b : c ? d : e</span></span>→<span><span>a ? b : (c ? d : e)</span></span>(ternary operator).
