Previous: 5.4 Operators and Expression Evaluation
Up: 5.4 Operators and Expression Evaluation
Next: 5.4.2 The Data Type of the Result
Previous Page: 5.4 Operators and Expression Evaluation
Next Page: 5.4.2 The Data Type of the Result
The data type and the value of an expression depends on the data types of the
operands and the order of evaluation of operators which is determined
by the precedence and associativity of operators.
Let us first consider the order of evaluation. When expressions contain more
than one operator, the order in which the operators are evaluated depends on
their precedence levels. A higher precedence operator is evaluated before a
lower precedence operator. If the precedence levels of operators are the same,
then the order of evaluation depends on their associativity (or, grouping). In
Chapter we briefly discussed the precedence
and associativity of arithmetic operators.
Table 5.1 shows the precedence levels and
associativity of all C operators.
In the table, there are 15
precedence levels 0 through 14: higher level implies higher precedence.
The precedence levels of operators are separated by solid lines with operators
within solid lines having the same precedence level. For example, binary
arithmetic operators ,
, and %
have the same precedence level which is higher
than binary +, and
.
Observe that the precedence of the assignment operator is lower than
all but the ``comma'' operator (described below).
This is in accordance with the rule that the expression on the
right side of an assignment is evaluated first,
and then its value is assigned to
the left hand side object. On the other hand, ``function call'' has the highest
precedence, since a function value is treated like a variable
reference in an expression.
In any expression, parentheses may be used to over ride the precedence of
the operators - innermost parentheses are always evaluated first.
The precedence of binary logical operators is lower than that of binary
relational operators; that of binary relational operators is lower than that of
binary arithmetic operators, and so forth. The unary NOT operator has a
precedence higher than that of all binary operators.
When operators of the same precedence level appear in an expression, the order of evaluation is determined by the associativity. Except for the assignment operator, associativity of most binary operators is left to right; associativity of the assignment operator and most unary operators is right to left. Consider the following program fragment:
int x = 10, y = 7, z = 20;
When a logical operator is used in an expression, the entire expression is not evaluated if the result of the entire logical expression is clear. For example,
(x > 0) && (y > 0) (x > 0) || (y > 0)In the first expression, if x > 0 is False, there is no need to evaluate the second part of the logical AND expression since the AND operation will be False. Similarly, in the second expression, the logical OR expression is True if the first part, x > 0, is True; there is no need to evaluate the second part. C evaluates only those parts of a logical expression that are required in order to arrive at the result of the expression.
When in doubt as to the order of evaluation within an expression, parentheses may be used to ensure evaluation is performed as intended.