short circuit evaluation

franka at mmintl.UUCP franka at mmintl.UUCP
Wed Feb 4 09:48:36 AEST 1987


In article <3721 at teddy.UUCP> jpn at teddy.UUCP (John P. Nelson) writes:
>60 seconds with my copy of K&R yeilds this (Appendix A):
>
>  "The & operator is associative and expressions involving & may be
>   rearranged"
>
>  "Unlike &, && guarantees left-to-right evaluation; moreover, the second
>   operand is not evaluated if the first operand is zero"
>
>True, it is not explitly stated that & and | are not short circuited.  It
>also doesn't explicitly say that + and * are not short circuited!  However
>&& and || are EXPLICITY defined as having short circuit behavier, no other
>operators are described this way, so it is safe to assume that all other
>operators do NOT short-circuit.

As a general rule, whatever is not specified in the C description (whether K&R
or ANSI) is just that: not specified.  Just because certain operators are
described as having certain behavior does not guarantee that those operators
for which the behavior is not specified behave in the opposite way.

I would not write code which assumes that & and | are not short-circuited;
nor would I write code which assumes that + and * are not short-circuited.
(Although + really can't be.)  But then, I use side-effects of expressions
only in time-critical portions of the code, and only when the side effect
is *guaranteed* to work by the language specification or in pieces of code
marked as machine dependent.  (However, my tendency is to write machine
dependent code in assembler.)

Frank Adams                           ihnp4!philabs!pwa-b!mmintl!franka
Ashton-Tate          52 Oakland Ave North         E. Hartford, CT 06108



More information about the Comp.lang.c mailing list