Operators

Operator Example
Unary Operators Negation -a, address-of &a, dereference *a
Binary Operators Equality a==a, multiplication a*a
Ternary Operators a ? b : c

boolean_condition ? this_if_bc_is_true : this_if_bc_is_false

Arithmetic Operators

Operator Explanation
+a unary plus
-a unary minus
a + b
a - b
a * b
a / b
a % b modulo
~a bit-wise NOT
a & b bit-wise AND
a b
a ^ b bit-wise XOR
a << b bit-wise left shift
a >> b bit-wise right shift

Undefined behavior occurs when

Logical and relational operators

Operator Explanation
!a logical NOT
a && b logical AND (short-circuiting)
a
a == b equal to
a != b not equal to
a < b less than
a > b greater than
a <= b less than or equal to
a >= b greater than or equal to

Assignment operators

Operator Explanation
a = b simple assignment
a += b addition assignment (equal to a = a + b)
a -= b subtraction assignment
a *= b multiplication assignment
a /= b division assignment
a %= b modulo assignment
a &= b bit-wise AND assignment
a = b
a ^= b bit-wise XOR assignment
a <<= b bit-wise left shift assignment
a >>= b bit-wise right shift assignment
int a,b,c;
a = b = c = 42;
int computeValue(){
    return 0;
}
if(int d = computeValue()){
    //Do something if d !=0
}
else{
    //Do something if d == 0
    //d is usable in both
}

LValue and RValue

Increment / Decrement

Operator Explanation
++a prefix increment
–a prefix decrement
a++ postfix increment
a– postfix decrement
Precedence Operator Description Associativity
1 :: Scope resolution Left-to-right →
2 a++   a-- Suffix/postfix increment and decrement
type()   type{} Functional cast
a() Function call
a[] Subscript
.   -> Member access
3 ++a   --a Prefix increment and decrement Right-to-left ←
+a   -a Unary plus and minus
!   ~ Logical NOT and bitwise NOT
(type) C-style cast
*a Indirection (dereference)
&a Address-of
sizeof Size-of[note 1]
co_await await-expression (C++20)
new   new[] Dynamic memory allocation
delete   delete[] Dynamic memory deallocation
4 .*   ->* Pointer-to-member Left-to-right →
5 a*b   a/b   a%b Multiplication, division, and remainder
6 a+b   a-b Addition and subtraction
7 <<   >> Bitwise left shift and right shift
8 <=> Three-way comparison operator (since C++20)
9 <   <=   >   >= For relational operators < and ≤ and > and ≥ respectively
10 ==   != For equality operators = and ≠ respectively
11 a&b Bitwise AND
12 ^ Bitwise XOR (exclusive or)
13 | Bitwise OR (inclusive or)
14 && Logical AND
15 || Logical OR
16 a?b:c Ternary conditional[note 2] Right-to-left ←
throw throw operator
co_yield yield-expression (C++20)
= Direct assignment (provided by default for C++ classes)
+=   -= Compound assignment by sum and difference
*=   /=   %= Compound assignment by product, quotient, and remainder
<<=   >>= Compound assignment by bitwise left shift and right shift
&=   ^=   |= Compound assignment by bitwise AND, XOR, and OR
17 , Comma Left-to-right →