Mail Archives: djgpp/1997/08/08/11:04:59
Kenneth Loafman wrote:
> The best example of this is "if (p && *p == 'a') {}" where the NULLness
> of
> p would be checked in the first expression before deferencing it in the
> second expression.
>
> Although not guaranteed, most C's stop evaluation once an ORing
> expression
> returns true from one of its expressions. Another potential surprise.
Where in the world do you get this? ANSI C guarantees short circuiting
for both && and ||. Section 6.3.13:
Unlike the bitwise binary & operator, the && operator guarantees
left-to-right evaluation; there is a sequence point afte the
evaluation of the first operand. If the first operand compares equal
to 0, the second operand is not evaluated.
This guarantees short circuiting for those who now how read a standard.
And section 6.3.14 decribes short circuiting for || ("if the first operand
compares _unequal_ to 0 . . ." [emphasis mine]).
I have yet to see a C compiler which claims ANSI compliance which does
_not_ have short circuiting features for both && and ||. Short circuiting
isn't new in ANSI, nor in K&R; it was in traditional C, and in fact short
circuiting is the reason there is a distinction between the logical and
bitwise operators.
--
Erik Max Francis, &tSftDotIotE / email / mailto:max AT alcyone DOT com
Alcyone Systems / web / http://www.alcyone.com/max/
San Jose, California, United States / icbm / 37 20 07 N 121 53 38 W
\
"Love is not love which alters / when it alteration finds."
/ William Shakespeare, _Sonnets_, 116
- Raw text -