CONTENTS

Appendix D. OPERATOR PRECEDENCE

Operator precedence determines the order in which operators are applied to a value. C++ operators come in 18 precedence groups, which are presented in Table D.1. Those in group 1 have the highest precedence, and so on. If two operators apply to the same operand (something upon which an operator operates), the operator with the higher precedence applies first. If the two operators have the same precedence, C++ uses associativity rules to determine which operator binds more tightly. All operators in the same group have the same precedence and the same associativity, which is either left to right (L朢 in the table) or right to left (R朙 in the table). Left-to-right associativity means to apply the left-hand operator first, while right-to-left associativity means to apply the right-hand operator first.

Some symbols, such as * and &, are used for more than one operator. In such cases, one form is unary (one operand) and the other form is binary (two operands), and the compiler uses the context to determine which is meant. The table labels operator groups unary or binary for those cases in which the same symbol is used two ways.

Here are some examples of precedence and associativity:

3 + 5 * 6

The * operator has higher precedence than the + operator, so it is applied to the 5 first, making the expression 3 + 30, or 33.

120 / 6 * 5

Both / and * have the same precedence, but these operators associate from left to right. That means the operator to the left of the shared operand (6) is applied first, so the expression becomes 20 * 5, or 100.

char * str = "Whoa";
char ch = *str++;

The postfix ++ operator has higher precedence than the unary * operator. This means the increment operator operates upon str and not *str. That is, the operation increments the pointer, making it point to the next character, rather than altering the character pointed to. However, because ++ is the postfix form, the pointer is incremented after the original value of *str is assigned to ch. Therefore, this expression assigns the character W to ch and then moves str to point to the h character.

char * str = "Whoa";
char ch = *++str;

The prefix ++ operator and the unary * operator have the same precedence, but they associate right-to-left. So, again, str and not *str is incremented. Because the ++ operator is in prefix form, first str is incremented, then the resulting pointer is dereferenced. Thus, str moves to point to the h character, and the h character is assigned to ch.

Note that the table uses binary or unary in the Precedence column to distinguish between two operators that use the same symbol, such as the unary address operator and the binary bitwise AND operator.

Table D.1. C++ Operator Precedence and Associativity
Precedence Operator Assoc. Meaning
1 ::   Scope resolution operator
  (expression)   Grouping
2 () L朢 Function call
  ()   Value construction, that is, type (expr)
  []   Array subscript
  ->   Indirect membership operator
  .   Direct membership operator
  const_cast   Specialized type cast
  dynamic_cast   Specialized type cast
  reinterpret_cast   Specialized type cast
  static_cast   Specialized type cast
  typeid   Type identification
  ++   Increment operator, postfix
  --   Decrement operator, postfix
3 (all unary) ! R朙 Logical negation
  ~   Bitwise negation
  +   Unary plus (positive sign)
  -   Unary minus (negative sign)
  ++   Increment operator, prefix
  --   Decrement operator, prefix
  &   Address
  *   Dereference (indirect value)
  ()   Type cast, that is, (type) expr
  sizeof   Size in bytes
  new   Dynamically allocate storage
  new []   Dynamically allocate array
  delete   Dynamically free storage
  delete []   Dynamically free array
4 .* L朢 Member dereference
  ->*   Indirect member dereference
5 (all binary) * L朢 Multiply
  /   Divide
  ^   Modulus (remainder)
6 (all binary) + L朢 Addition
  -   Subtraction
7 << L朢 Left shift
  >>   Right shift
8 < L朢 Less than
  <=   Less than or equal to
  >=   Greater than or equal to
  >   Greater than
9 == L朢 Equal to
  !=   Not equal to
10 (binary) & L朢 Bitwise AND
11 ^ L朢 Bitwise XOR (exclusive OR)
12 | L朢 Bitwise OR
13 && L朢 Logical AND
14 || L朢 Logical OR
15 = R朙 Simple assignment
  *=   Multiply and assign
  /=   Divide and assign
  %=   Take remainder and assign
  +=   Add and assign
  -=   Subtract and assign
  &=   Bitwise AND and assign
  ^=   Bitwise XOR and assign
  |=   Bitwise OR and assign
  <<=   Left shift and assign
  >>=   Right shift and assign
16 :? R朙 Conditional
17 throw L朢 Throw exception
18 , L朢 Combine two expressions into one

 

CONTENTS