Operator precedence determines the order in which operators are evaluated in expressions. In C++, operators with higher precedence are evaluated before operators with lower precedence.
Precedence | Operator | Description |
---|---|---|
1 | :: |
Scope resolution |
2 | a++ , a-- |
Postfix increment and decrement |
3 | ++a , --a |
Prefix increment and decrement |
4 | + , - , ! , ~ |
Unary plus, unary minus, logical NOT, bitwise NOT |
5 | * , / , % |
Multiplication, division, modulus |
6 | + , - |
Addition, subtraction |
7 | << , >> |
Bitwise left shift, bitwise right shift |
8 | < , <= , > , >= |
Relational operators |
9 | == , != |
Equality and inequality |
10 | & |
Bitwise AND |
11 | ^ |
Bitwise XOR |
12 | ` | ` |
13 | && |
Logical AND |
14 | ` | |
15 | = , += , -= , *= , /= , %= , <<= , >>= , &= , ^= , ` |
=` |
16 | , |
Comma operator |
-
Scope resolution:
namespace MyNamespace { int x = 10; } int main() { int x = MyNamespace::x; // Uses the scope resolution operator }
-
Postfix vs Prefix:
int a = 5; int b = a++; // b = 5, a = 6 int c = ++a; // c = 7, a = 7
-
Multiplication before Addition:
int result = 3 + 4 * 2; // result = 11, not 14
-
Logical AND before Logical OR:
bool result = true || false && false; // result = true, because && has higher precedence than ||
-
Using Parentheses to Override Precedence:
int result = (3 + 4) * 2; // result = 14, parentheses override the default precedence
Remember, when in doubt, it's always a good practice to use parentheses to make the order of operations explicit.