C++ Operator Summary

The Simplest Precedence Rule of All

1. *, /, % come before +, -
2. parenthesize everything else

The following precedence chart is a simplified version of the table given in the operator summary of The C++ Programming Language, by Bjarne Stroustrup, the creator of C++.

Each box holds operators at the same precedence. Operators in higher boxes have higher precedence. Therefore, a + b * c means a + (b * c) not (a + b) * c. In the same way, a++ + i means (a++) + i.

Unary operators and assignment operators are right-associative; all other operators are left-associative. Therefore, a = b = c means (a = (b = c), and a + b + c means (a + b) + c.

C++ Operator Precedence Chart
(chart for printing)

Description Operator Use
scope resolution :: std::cout
post increment
post decrement
++
--
k++
k--
pre increment
pre decrement
not
unary minus
unary plus
++
--
!
-
+
++k
--k
! isPrime()
-5
+23
multiply
divide
modulo (remainder)
*
/
%
x * y
i / j
m % n
add
subtract
+
-
x + y
i - j
put
get
<<
>>
cout << k;
cin >> i >> j;
less than
less than or equal
greater than
greter than or equal
<
<=
>
>=
2 < 3
ch1 <= ch2
i > k
x >= z
equal
not equal
==
!=
5 == num1
age != 65
AND && p && q
OR || p || q
conditional operator ?: (age < 21)? 0 : 1
assignment operators =
+=
-=
. . .
k = k + 1
k += 1
k -= 1
. . .

Rule for simple expressions in C++

Yet another way to remember the order of operations can be summed up by this mnemonic: PMDRAS ("Please, my deaR Aunt Sally!", or, "Please, my deaR! -- Aunt Sally"):

Which means, evaluate expressions in parentheses first, then multiplication and division (left to right), and then addition and subtraction (left to right). If parentheses are enclosed within other parentheses, evaluate the innermost first.