Expression sequencing query

Wayne Throop throopw at dg_rtp.UUCP
Tue Sep 30 08:07:43 AEST 1986


> drw at cullvax.UUCP (Dale Worley)
>> tomc at oakhill.UUCP (Tom Cunningham)

>> 	/* a = b + b + b */
>> 	a = ((b=1),b) + ((b=2),b) + ((b=3),b)
>>
>> I expected the result to be 6.

> Harbison&Steele (7.11) makes it clear that an implementation must
> evaluate one argument of a binary operator completely before starting
> evaluation of the other argument.  Thus, the result should be 6.

True.  But note what H&S say at the end of section 7.11:

    The matter of interleaving was not discussed [in the original
    description of C...]  We advise implementors to adhere [to the
    non-interleaving rule.]  We also advise programmers not to exploit
    [this rule] too cleverly.

And, as we have seen, their second bit of advice was well taken, since
their first bit of advice seems to have been ignored in almost all
implementations.

> I don't know what the ANSI standard says.

And the clincher is that ANSI didn't go along with H&S on this point.
They say that expressions separated by "sequence points" may not be
interleaved.  These sequence points occur between "full expressions".
Full expressions are initializers, expression statements, expressions in
"if", "while" and the like, and expressions in "return".  (I suspect
that (?:) and (,) expressions have sequence points also, but couldn't
find this on a trivial inspection of the draft standard.) Thus, ANSI C
says that 9 is as good as 6, and repudiates H&S.  So it goes.

--
Sometimes I think the only universal in the computing field is the
fetch-execute cycle.
    --- Alan J. Perlis
-- 
Wayne Throop      <the-known-world>!mcnc!rti-sel!dg_rtp!throopw



More information about the Comp.lang.c mailing list