What does ANSI C say about short circuit evaluation?

Henry Spencer henry at zoo.toronto.edu
Fri May 24 03:02:32 AEST 1991


In article <1991May22.092404.25297 at ucthpx.uct.ac.za> gram at uctcs.uucp (Graham Wheeler) writes:
>i) Does ANSI C say that compilers can rearrange the order of expression 
>	evaluation?

Not unless it can guarantee that the results (including things like
overflow exceptions, if any) are the same.  This is a tightening-up
of the specs relative to old C.

>ii) Does it say that Boolean expressions must be evaluated with short-
>	circuit evaluation?

If you're referring to the && and || operators, C has *always* required 
short-circuit evaluation of these, and ANSI C confirms this.  If you're
referring to the & and | operators, short-circuit evaluation is forbidden
unless the results (including side effects) are exactly the same.
-- 
And the bean-counter replied,           | Henry Spencer @ U of Toronto Zoology
"beans are more important".             |  henry at zoo.toronto.edu  utzoo!henry



More information about the Comp.lang.c mailing list