Operator Precedence

Topic

What is Operator precedence in C++?

Explanation

Precedence of Operators:


An Operator Precedence is the hierarchy in which operators are evaluated. For example in the expression 2 + 2 * 3, the result would be 8 since the "*" operator has higher precedence over the "+" operator.

Now the same expression is used with a paranthesis like (2 + 2) * 3, the result would be 12. The "()" is used to force precedence by making the expression inside the braces executed first. If the precedence are equal then the associativity is used to evaluate an expression.
This is the Operator Precedence in C++.
Name Operator Direction Precedence
Parentheses () Left to Right 1
Post-increment ++ Left to Right 2
Post-decrement -- Left to Right 2
Address & Right to Left 2
Bitwise NOT ~ Right to Left 2
Typecast (type) Right to Left 2
Logical NOT ! Right to Left 2
Negation - Right to Left 2
Plus Sign + Right to Left 2
Pre-increment ++ Right to Left 2
Pre-decrement -- Right to Left 2
Size of data sizeof Right to Left 2
Modulus % Left to Right 3
Multiplication * Left to Right 3
Division / Left to Right 3
Addition + Left to Right 4
Subtraction - Left to Right 4
Bitwise Shift Left << Left to Right 5
Bitwise Shift Right >> Left to Right 5
Less Than < Left to Right 6
Less Than or Equal <= Left to Right 6
Greater Than > Left to Right 6
Greater Than or Equal >= Left to Right 6
Equal == Left to Right 7
Not Equal != Left to Right 7
Bitwise AND & Left to Right 8
Bitwise XOR ^ Left to Right 9
Bitwise OR | Left to Right 10
Logical AND && Left to Right 11
Logical OR || Left to Right 12
Condition Expression ?: Right to Left 13
Assignment = Right to Left 14
Additive Assignment += Right to Left 14
Subtractive Assignment -= Right to Left 14
Multiplicative Assignment *= Right to Left 14
Divisional Assignment /= Right to Left 14
Modulating Assignment %= Right to Left 14
Left Shift Assignment >>= Right to Left 14
Right Shift Assignment <<= Right to Left 14
AND Assignment &= Right to Left 14
XOR Assignment |= Right to Left 14
OR Assignment ^= Right to Left 14
Comma , Left to Right 15

C++ Tutorial