An Operator is a special symbol that tells the compiler to perform specific mathematical, relational, or logical manipulations on operands. C++ features a rich variety of operators with strict precedence and associativity rules.
+, -, *, /, % (modulus).==, !=, >, <, >=, <=, and C++20 Three-way comparison <=> (spaceship operator).&& (AND), || (OR), ! (NOT).&, |, ^ (XOR), ~ (NOT), << (left shift), >> (right shift).=, +=, -=, *=, /=, %=, &=, |=.condition ? expr1 : expr2.| Precedence Level | Operators | Associativity |
|---|---|---|
| 1 (Highest) | Scope resolution :: |
Left to Right |
| 2 | Member access ., ->, Subscript [], Function call (), Postfix ++, -- |
Left to Right |
| 3 | Prefix ++, --, Unary +, -, Logical NOT !, Bitwise NOT ~, Dereference *, Address-of &, sizeof |
Right to Left |
| 4 | Multiplication *, Division /, Modulo % |
Left to Right |
| 5 | Addition +, Subtraction - |
Left to Right |
| 6 | Bitwise Shift <<, >> |
Left to Right |
| 7 | Relational <, <=, >, >= |
Left to Right |
| 8 | Equality ==, != |
Left to Right |
| 9 | Logical AND && |
Left to Right |
| 10 | Logical OR || |
Left to Right |
| 11 | Ternary Conditional ? : |
Right to Left |
| 12 (Lowest) | Assignment =, +=, -=, Comma , |
Right to Left |
Boost your C++ proficiency by studying these closely connected core modules:
Teach your custom classes how to use +, -, << and ==.
Using relational and logical operators in decision making.
Multi-way branching based on integral operators.