Expression Sequencing Query

Fabbian G. Dufoe fgd3 at jc3b21.UUCP
Fri Oct 3 11:51:56 AEST 1986


     Earlier this week I posted the following (Message-ID: <468 at jc3b21.UUCP>):

>      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

     This evening I received the following mail:

> While I disagree about the "required" result for the first expression,
> I have no doubt that 6 and 6 are required for the second two, and I
> believe that our 3B2 compilers give those results.  The value of an
> assignment or assignment-op is always the value of its left side.
> I would appreciate it if you would double-check your result and post
> a correction on the net.
> 
> Dave Kristol
> AT&T
> ...akgua!acguc!codas!sfbc!dmk

     OK, Dave.  I compiled and ran the program again on the 3B2.  It
produced 9, 9, and 6, just as I said the first time.  Now let me take issue
with the errors in your note.

     (1) There is no significant difference between the first two expressions.
Whatever reason you have to disagree about the required result for the
first should apply to the second as well.  Thus ((b=1),b) doesn't do
anything in this case which (b=1) doesn't do.

     (2) I reviewed K&R for some light on what result the first and second
expressions should produce.  I found the following sentence on page 49:
"As mentioned before, expressions involving one of the associative and
commutative operators (*, +, &, ^, |) can be rearranged even when
parenthesized."  And people call BASIC brain-damaged!  This means
the results of the first two expressions are unpredictable without
knowledge of the specific C compiler involved.  Furthermore, it implies the
following expression is unpredictable:

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

Incredible as it may seem, the 3B2 thinks "a" will be 9.  (The Amiga,
with Lattice, thinks "a" will be 7.)  I believe that is a serious flaw in the
language definition.  Two C compilers, both correctly following the
definition in K&R, can compile the same legal C expression and come up
with different results.  Oh, well.  Who said C was a portable language?

     (3) You said you believe the 3B2 produces the results you thought
correct.  Why didn't you check?  Your reasoning seemed to be "I think the
correct results are 9, 6, and 6.  I think the 3B2 compiler is always right.
Therefore the results must have been 9, 6, and 6."  To my thinking, the
proper response would have been "Maybe there is something wrong here.  I'll
compile the program and see the results for myself.  Hmm...The results are
9, 9, and 6.  I wonder what's wrong with the compiler or my understanding
of C?"  Do you see the difference?

     This has certainly been an instructive exercise for me.  It is
important to understand peculiarities like that.  If I hadn't gone into it
this carefully I'd have been bitten later on when the result was critical.
Thanks for making me take such a hard look at the question.

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