20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

442 Appendix A C Language Summary<br />

F<strong>in</strong>ally, because the assignment operators group from right to left, the statement<br />

a = b = 0;<br />

is evaluated as<br />

a = (b = 0);<br />

which has the net result of sett<strong>in</strong>g the values of a and b to 0. In the case of the<br />

expression<br />

x[i] + ++i<br />

it is not def<strong>in</strong>ed whether the compiler evaluates the left side of the plus operator or the<br />

right side first. Here, the way that it’s done affects the result because the value of i might<br />

be <strong>in</strong>cremented before x[i] is evaluated.<br />

Another case <strong>in</strong> which the order of evaluation is not def<strong>in</strong>ed is <strong>in</strong> the follow<strong>in</strong>g<br />

expression:<br />

x[i] = ++i<br />

In this situation, it is not def<strong>in</strong>ed whether the value of i is <strong>in</strong>cremented before or after<br />

its value is used to <strong>in</strong>dex <strong>in</strong>to x.<br />

The order of evaluation of function arguments is also undef<strong>in</strong>ed.Therefore, <strong>in</strong> the<br />

function call<br />

f (i, ++i);<br />

i might be <strong>in</strong>cremented first, thereby caus<strong>in</strong>g the same value to be sent as the two arguments<br />

to the function.<br />

The C language guarantees that the && and || operators are evaluated from left to<br />

right. Furthermore, <strong>in</strong> the case of &&, it is guaranteed that the second operand is not<br />

evaluated if the first is 0; and <strong>in</strong> the case of ||, it is guaranteed that the second operand is<br />

not evaluated if the first is nonzero.This fact is worth bear<strong>in</strong>g <strong>in</strong> m<strong>in</strong>d when form<strong>in</strong>g<br />

expressions such as<br />

if ( dataFlag || checkData (myData) )<br />

...<br />

because, <strong>in</strong> this case, checkData is called only if the value of dataFlag is 0.To take<br />

another example, if the array a is def<strong>in</strong>ed to conta<strong>in</strong> n elements, the statement that<br />

beg<strong>in</strong>s<br />

if (<strong>in</strong>dex >= 0 && <strong>in</strong>dex < n && a[<strong>in</strong>dex] == 0))<br />

...<br />

references the element conta<strong>in</strong>ed <strong>in</strong> the array only if <strong>in</strong>dex is a valid subscript <strong>in</strong> the<br />

array.<br />

5.2 Constant Expressions<br />

A constant expression is an expression <strong>in</strong> which each of the terms is a constant value.<br />

Constant expressions are required <strong>in</strong> the follow<strong>in</strong>g situations:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!