C and C++ Operator Precedence and Associativity


This table lists the C and C++ language operators in order of precedence and shows the direction of associativity for each operator. Operators that exist only in C++ are shown in red. Operators that appear in the same group have the same precedence.

Operator precedence determines which operator will be performed first in a group of operators with different precedences. For instance 5 + 3 * 2 is calculated as 5 + (3 * 2), giving 11, and not as (5 + 3) * 2, giving 16.

The operator associativity rules define the order in which adjacent operators with the same precedence level are evaluated. For instance the expression 8 - 3 - 2 is calculated as (8 - 3) - 2, giving 3, and and not as 8 - (3 - 2), giving 7. In this case we say that subtraction is left associative meaning that the left most subtraction must be done first.

Operator Name  Associativity  Operators
Primary scope resolution left to right ::
Primary   left to right  ()  [ ]  .  -> dynamic_cast typeid
Unary  right to left  ++  --  +  -  !  ~  &  *  (type_name)  sizeof new delete
C++ Pointer to Member left to right .*->*
Multiplicative  left to right  *  /  %
Additive  left to right  +  -
Bitwise Shift  left to right  <<  >>
Relational  left to right  <  >  <=  >=
Equality  left to right  ==  !=
Bitwise AND  left to right  &
Bitwise Exclusive OR  left to right  ^
Bitwise Inclusive OR  left to right  |
Logical AND  left to right  &&
Logical OR  left to right  ||
Conditional  right to left  ? :
Assignment  right to left  =  +=  -=  *=   /=  <<=  >>=  %=   &=  ^=  |=
Comma  left to right  ,