Expression sequencing query

Fabbian G. Dufoe fgd3 at jc3b21.UUCP
Tue Sep 30 04:56:05 AEST 1986


In article <111 at titan.UUCP>, eectrsef at titan.UUCP 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?
> >
> Tom, I agree, the result should be 6, as defined by K&R, but I have tried
> it on a Cyber 180/830 running NOS VE, and get 9, also AT&T's 3B5
> System V, gets 9. 
> I would like everyone to test it on as many machines as prossible, to
> see if we can find as least ONE other besides Small-C, that works.
> 
> Mike Stump  ucbvax!hplabs!csun!csunb!beusemrs

     I compiled the following code on an AT&T 3B2 (System V) and an
Amiga (Lattice 3.03):

main()
{
     int a, b;
     a = ((b=1),b) + ((b=2),b) + ((b=3),b);
     printf("%d\n", a);
     a = (b=1) + (b=2) + (b=3);
     printf("%d\n", a);
     a = (b=1), a += (b=2), a += (b=3);
     printf("%d\n", a);
}

     On the 3B2 it produced:

9
9
6

     On the Amiga, surprisingly, it produced:

7
7
6

     Both are wrong, but one can see where the 3B2 went wrong.  How in
the world did Lattice come up with 7?

Fabbian Dufoe
  350 Ling-A-Mor Terrace South
  St. Petersburg, Florida  33705
  813-823-2350

UUCP: ...akgua!akguc!codas!peora!ucf-cs!usfvax2!jc3b21!fgd3 



More information about the Comp.lang.c mailing list