Expression sequencing query
Mike Meyer
mwm at eris.berkeley.edu
Fri Sep 26 19:27:59 AEST 1986
In article <353 at cullvax.UUCP> drw at cullvax.UUCP (Dale Worley) writes:
>> In article <760 at oakhill.UUCP> tomc at oakhill.UUCP (Tom Cunningham) writes:
>> > /* a = b + b + b */
>> > a = ((b=1),b) + ((b=2),b) + ((b=3),b)
>> >
>> >I expected the result to be 6. With the Microsoft C compiler and the
>> >compiler on the Sun 3, the result is 9. Apparently the parenthetical
>> >assignments are all getting done before the comma and addition. Any
>> >thoughts on this?
>
>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. I
>don't know what the ANSI standard says.
My reading of the ANSI hardcopy is that it doesn't say. I thought H&S
was a description, not a definition.
>Dec VAX Ultrix gives 9.
As do VAX 4.2 and 4.3.
>Lattice C 3.00 for MS-DOS gives 7!!! (Yes, that's "7", not a typo!)
Some messy - and almost believable - arguments can be made that the
value of that expression is "implementation dependent". Rather than do
that, I'll just point out that *ANY* time++ you have a single
statement that changes a variable, then uses the variable in a
different place, you're asking for trouble. For that particular
expression, I'd expect the following, with increasing surprise as you
move down the list:
6
3, 9
4, 5, 7, 8
other integers representable on the machine
NANs of various flavors
dropped cores.
<mike
++ Except for those cases that are in different operands of a logical
operator, as the evaluation order on those is known.
More information about the Comp.lang.c
mailing list