conditional expression evaluation question

Henry Spencer henry at utzoo.UUCP
Wed Jan 14 07:23:55 AEST 1987


>		if ((*cp++ | *cp++ | *cp++) == 0) ++triples;
> ...
> ...  My question is "Does C guarantee execution of portions of a
> conditional expression, even when the result is known after partial
> evaluation?"...

Basically, the answer is "no".  You get very few guarantees about the
order of execution or even presence of execution in C.  It is possible
that X3J11 mandates complete execution, and a good many compilers will
do it, simply because other sloppy people have made similar unwise
assumptions in the past and it causes too much trouble to violate them.
But I would suggest that it is extremely unwise to count on the above
code doing what you think it does.  (For another example, are you sure
that "*cp++" is atomic, i.e. that the postincrement will be done before
the next part of the expression?  I would think that an unwise assumption
too.)  There is too much chance of a "clever" compiler fouling things up.

Incidentally, not everyone would agree that the above is cleaner than
using "*(cp+1)" etc.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,decvax,pyramid}!utzoo!henry



More information about the Comp.lang.c mailing list