Operator precedence and associativity determine the order in which operators are evaluated. Suppose that you have this expression:
3 + 4 * 4 > 5 * ( 4 + 3 ) 1
What is its value? How does the compiler know the execution order of the operators? The expression in the parentheses is evaluated first. (Parentheses can be nested, in which case the expression in the inner parentheses is executed first.) When evaluating an expression without parentheses, the operators are applied according to the precedence rule and the associativity rule. The precedence rule defines precedence for operators, as shown in Table 3.10, which contains the operators you have learned so far. Operators are listed in
| Precedence | Operator |
|---|---|
|
|
var++
and
var ” ”
(Postfix)
+ , - (Unary plus and minus), ++var and ” ”var (Prefix) (type) (Casting) ! (Not) * , / , % (Multiplication, division, and remainder) + , - (Binary addition and subtraction) < , <= , > , >= (Comparison) == , != (Equality) & (Unconditional AND) ^ (Exclusive OR) (Unconditional OR) && (Conditional AND) (Conditional OR) = , += , “= , *= , /= , %= (Assignment operator) |
If operators with the same precedence are
Assignment operators are right-associative . Therefore, the expression
Suppose a , b , and c are 1 before the assignment; after the whole expression is evaluated, a becomes 6 , b becomes 6 , and c becomes 5 . Note that left associativity for the assignment operator would not make sense.
Applying the operator precedence and associativity rule, the expression
3 + 4 * 4 > 5 * (4 + 3) - 1
is evaluated as
Tip
|
|
You can use parentheses to force an evaluation order as well as to make a program easy to read. Use of redundant parentheses does not slow down the execution of the expression. |
The precedence and associativity rules specify the order of the operators but not the order in which the operands of a binary operator are evaluated. Operands are evaluated
a + b * (c +
10
* d) / e
a , b , c , d , and e are evaluated in this order. If no operands have side effects that change the value of a variable, the order of operand evaluation is irrelevant. Interesting cases arise when operands do have a side effect. For example, x becomes 1 in the following code because a is evaluated to 0 before ++a is evaluated to 1 .
int a = ; int x = a + (++a);
But x becomes 2 in the following code because ++a is evaluated to 1 , and then a is evaluated to 1 .
int a = ; int x = ++a + a;
The order for evaluating operands takes precedence over the operator precedence rule. In the former case, (++a) has higher precedence than addition ( + ), but since a is a left-hand operand of the addition ( + ), it is evaluated before any part of its right-hand operand (e.g., ++a in this case).
In summary, the rule of evaluating an expression is:
Rule 1: Evaluate whatever
Rule 2: The operators are applied according to their precedence, as shown in Table 3.10.
Rule 3: The associativity rule applies for two operators
Applying the rule, the expression
3 + 4 * 4 > 5 * (4 + 3) - 1
is evaluated as
The result happens to be the same as applying Rule 2 and Rule 3 without applying Rule 1. In fact, Rule 1 is not necessary if no operands have side effects that change the value of a variable in an expression.