conditional expression evaluation question

Colin Jenkins ag0 at k.cc.purdue.edu
Thu Jan 15 05:23:37 AEST 1987


In article <207 at rebel.UUCP> george at rebel.UUCP (George M. Sipe) writes:
>
>I need to check a string, composed of byte triples, for a null area no
>less than MINSKIP triples in length.  A pointer, cp, is initialized to
>a triplet boundary.  After the test, it must remain on a triplet
>boundary.  Initially, I wrote the following:
>
>	while (cp < end && triples < MINSKIP)
>		if ((*cp++ | *cp++ | *cp++) == 0) ++triples;
>		else triples = 0;
>
>After looking at it, I wasn't absolutely sure that it would perform as
>expected.  My question is "Does C guarantee execution of portions of a
>conditional expression, even when the result is known after partial
>evaluation?".  

Actually, you have only one conditional expression in your if statement.  The
"|" is a bitwise or and not a logical connective.  If that is what you
intended then you don't have to worry since you have only one conditional
expression.

The answer to you question about evaluation is no however.  From page 38 of 
K&R "The C Programming Language":

	"Expressions connected by the && or || are evaluated left to right,
	 and evaluation stops as soon as the truth or falsehood of the result
	 is known."


				Colin



More information about the Comp.lang.c mailing list