Operator precedence and associativity
Operator precedence defines the order in which operators are evaluated in expressions with multiple operators.
Operators with higher precedence will be bound tighter (as if by parentheses) to its argument than the operators with lower precedence. For example, the expression a + b * c is equivalent to a + (b * c). That is, we firstly evaluate the multiplication operator and only then sum the result of the multiplication and the value of
a. This is because the multiplication operator has higher precedence.
If there are multiple operators of the same precedence in the expression, the order of evaluation is defined by their associativity: either right-to-left or left-to-right. For example, a + b - c is equivalent to (a + b) - c as associativity of these particular operators is left to right.
The following table lists the precedence and associativity of C++ operators.
Operators are listed top to bottom, in descending precedence. in an
||Suffix/postfix increment and decrement|
||Element selection by reference|
||Element selection through pointer|
||Prefix increment and decrement||Right-to-left|
||Unary plus and minus|
||Logical NOT and bitwise NOT|
||Dynamic memory allocation|
||Dynamic memory deallocation|
||Pointer to member||Left-to-right|
||Multiplication, division, and remainder|
||Addition and subtraction|
||Bitwise left shift and right shift|
||For relational operators < and ≤ respectively|
||For relational operators > and ≥ respectively|
||For relational = and ≠ respectively|
||Bitwise XOR (exclusive or)|
||Bitwise OR (inclusive or)|
||Direct assignment (provided by default for C++ classes)|
||Assignment by sum and difference|
||Assignment by product, quotient, and remainder|
||Assignment by bitwise left shift and right shift|
||Assignment by bitwise AND, XOR, and OR|
||Throw operator (for exceptions)|