Objectives
- Use operators in expressions
- Create correct expressions using operator precedence
The operators in the table below are listed in descending order of precedence. The highlighted operators are the operators that we will use in this course. Just as in algebra, parentheses can be used to change the order of precedence.
Level | Operator | Description | Associativity |
---|---|---|---|
0 | :: | scope resolution | Left to Right |
1 | ++ | post-increment | Left to Right |
-- | post-decrement | ||
() | function call | ||
[] | array subscripting | ||
. | member selection via reference | ||
-> | member selection via pointer | ||
typeid() | run-time type info | ||
const_cast | type cast | ||
dynamic_cast | type cast | ||
reinterpret_cast | type cast | ||
static_cast | type cast | ||
2 | ++ | pre-increment | Right to Left |
-- | pre-decrement | ||
+ | unary plus | ||
- | unary minus | ||
! | logical NOT | ||
~ | bitwise NOT | ||
(type) | type cast | ||
* | dereference | ||
& | address-of | ||
sizeof | size-of | ||
new | dynamic memory allocation | ||
new[] | dynamic array memory allocation | ||
delete | dynamic memory deallocation | ||
delete[] | dynamic array memory deallocation | ||
3 | .* | pointer to member using reference | Left to Right |
->* | pointer to member using pointer | ||
4 | * | multiplication | Left to Right |
/ | division | ||
% | modulus (remainder) | ||
5 | + | addition | Left to Right |
- | subtraction | ||
6 | << | bitwise left shift | Left to Right |
>> | bitwise right shift | ||
7 | < | relational: less than | Left to Right |
<= | relational: less than or equal | ||
> | relational: greater than | ||
>= | relational: greater than or equal | ||
8 | == | relational: equal | Left to Right |
!= | relational: not equal | ||
9 | & | bitwise AND | Left to Right |
10 | ^ | bitwise XOR | Left to Right |
11 | | | bitwise OR | Left to Right |
12 | && | logical AND | Left to Right |
13 | || | logical OR | Left to Right |
14 | c?t:f | conditional (ternary) | Right to Left |
15 | = | assignment | Right to Left |
+= | addition and assignment | ||
-= | subtraction and assignment | ||
*= | multiplication and assignment | ||
/= | division and assignment | ||
%= | remainder and assignment | ||
<<= | bitwise left shift and assignment | ||
>>= | bitwise right shift and assignment | ||
&= | bitwise AND and assignment | ||
^= | bitwise XOR and assignment | ||
|= | bitwise OR and assignment | ||
16 | throw | throw an exception | N/A |
17 | , | list separator | Left to Right |
C++ also includes keywords as synonyms for some operators. They are as follows:
keyword | synonym for |
---|---|
and | && |
and_eq | &= |
bitand | & |
bitor | | |
compl | ~ |
not | ! |
not_eq | != |
or | || |
or_eq | |= |
xor | ^ |
xor_eq | ^= |