Programming in C

(Barry) #1
442 Appendix A C Language Summary

Finally, because the assignment operators group from right to left, the statement
a = b = 0;
is evaluated as
a = (b = 0);
which has the net result of setting the values of aand bto 0. In the case of the
expression
x[i] + ++i
it is not defined whether the compiler evaluates the left side of the plus operator or the
right side first. Here, the way that it’s done affects the result because the value of imight
be incremented before x[i]is evaluated.
Another case in which the order of evaluation is not defined is in the following
expression:
x[i] = ++i
In this situation, it is not defined whether the value of iis incremented before or after
its value is used to index into x.
The order of evaluation of function arguments is also undefined.Therefore, in the
function call
f (i, ++i);
imight be incremented first, thereby causing the same value to be sent as the two argu-
ments to the function.
The C language guarantees that the &&and ||operators are evaluated from left to
right. Furthermore, in the case of &&, it is guaranteed that the second operand is not
evaluated if the first is 0 ; and in the case of ||, it is guaranteed that the second operand is
not evaluated if the first is nonzero.This fact is worth bearing in mind when forming
expressions such as
if ( dataFlag || checkData (myData) )

because, in this case,checkDatais called only if the value of dataFlagis 0 .To take
another example, if the array ais defined to contain nelements, the statement that
begins
if (index >= 0 && index < n && a[index] == 0))

references the element contained in the array only if indexis a valid subscript in the
array.

5.2 Constant Expressions


A constant expression is an expression in which each of the terms is a constant value.
Constant expressions are requiredin the following situations:

20 0672326663 AppA 6/10/04 2:01 PM Page 442

Free download pdf