🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.c
Article #9453

Re: Expression sequencing query

#9453
From: barada
Date: Sun, 28 Sep 1986 02:26
52 lines
2326 bytes
/* Written  4:08 pm  Sep 24, 1986 by cullvax.UUCP!drw in maggot:net.lang.c */
> In article <760@oakhill.UUCP> tomc@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.

Dec VAX Ultrix gives 9.

Lattice C 3.00 for MS-DOS gives 7!!!  (Yes, that's "7", not a typo!)

Dale
/* End of text from maggot:net.lang.c */

I rearraged the above to

	a = (b,(b=1)) + (b,(b=2)) + (b,(b=3));

And my BSD4.2 VAX produced:

        movl    $1,_b
        movl    _b,r0
        movl    $2,_b
        movl    _b,r1
        addl2   r1,r0
        movl    $3,_b
        movl    _b,r1
        addl2   r1,r0
        movl    r0,_a
									 
As you can see, the comma operator is evaluated right to left. I think that
this is a serious bug.

BTW, this code produces the proper answer of 6.

--
Peter Barada                                   | (617)-671-9905
Applicon, Inc. A division of Schlumberger Ltd. | Billerica MA, 01821

UUCP: {allegra|decvax|mit-eddie|utzoo}!linus!raybed2!applicon!barada
      {amd|bbncca|cbosgd|wjh12|ihnp4|yale}!ima!applicon!barada
      
	Sanity is yet another perversion of reality.
Technical Details
Message-ID: <11900001@maggot>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!lll-crg!lll-lcc!styx!twg-ap!amdahl!pyramid!ucat!pesnta!amd!amdcad!amdimage!prls!philabs!linus!raybed2!applicon!bambi!maggot!barada
References: <111@titan.UUCP>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!mnetor!seismo!lll-crg!lll-lcc!styx!twg-ap!amdahl!pyramid!ucat!pesnta!amd!amdcad!amdimage!prls!philabs!linus!raybed2!applicon!bambi!maggot!barada
From: barada@maggot.applicon.UUCP
Newsgroups: net.lang.c
Subject: Re: Expression sequencing query
Message-ID: <11900001@maggot>
Date: Sat, 27-Sep-86 22:26:00 EDT
Article-I.D.: maggot.11900001
Posted: Sat Sep 27 22:26:00 1986
Date-Received: Sat, 4-Oct-86 01:31:08 EDT
References: <111@titan.UUCP>
Lines: 52
Nf-ID: #R:titan.UUCP:-11100:maggot:11900001:000:1627
Nf-From: maggot.applicon.UUCP!barada    Sep 27 22:26:00 1986