C++ Operator Precedence
Operator precedence in C++ determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated first, followed by operators with lower precedence. When operators have the same precedence, their evaluation order is determined by their associativity.
The following is a list of the C++ operators, arranged in order of decreasing precedence:
- Scope resolution operator (::)
- Postfix increment (++) and decrement (--)
- Function call operator ()
- Member selection operator (.)
- Member selection operator (->)
- Type cast operator (dynamic_cast, static_cast, reinterpret_cast, const_cast)
- Unary operators (+, -, !, ~, ++, --, *, &, sizeof, new, delete)
- Multiplicative operators (*, /, %)
- Additive operators (+, -)
- Shift operators (<<, >>)
- Relational operators (<, >, <=, >=)
- Equality operators (==, !=)
- Bitwise AND operator (&)
- Bitwise XOR operator (^)
- Bitwise OR operator (|)
- Logical AND operator (&&)
- Logical OR operator (||)
- Conditional operator (?:)
- Assignment operators (=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=)
- Comma operator (,)
It is important to note that parentheses can be used to change the order of evaluation in an expression. Expressions within parentheses are evaluated first, regardless of operator precedence.
In summary, operator precedence determines the order in which operators are evaluated in an expression. Understanding operator precedence is important to write correct and readable code.