From: steve@unidata.ucar.edu (Steve Emmerson)
Subject: Re: C compiler problem
Date: 1995/09/08 [thread overview]
Message-ID: <steve.810574681@buddy.unidata.ucar.edu> (raw)
In-Reply-To: KADHIM.95Sep6103902@munge.cs.colorado.edu
kadhim@munge.cs.colorado.edu (Basim Kadhim) writes:
>The Alpha C compiler behaves quite strangely with comma expressions.
>Consider the following example:
>#include <stdio.h>
>int
>add (a, b)
>{
> return a + b;
>}
>int
>main ()
>{
> int x;
> printf("%d\n", add((x=5,x),(x=4,x)));
>}
Because the above program doesn't conform to the C Standard, it is
inappropriate to say the the compiler is behaving `strangely'. It is
more correct to say that the program is relying upon undefined behavior.
The program doesn't conform to the C Standard because the value of `x'
is being modified more than once between adjacent sequence points.
This violates the `shall' constraint imposed by Section 3.3 of the
Standard. The argument-separating comma is not the comma operator
(Section 3.3.17); thus, there is no sequence point separating the
evaluation of the two arguments to the add() function; consequently, the
compiler may pretty much do what it wishes to optimize evaluation.
Moral: if you don't want `strange' behavior, then write conforming code.
--
Steve Emmerson steve@unidata.ucar.edu ...!ncar!unidata!steve
prev parent reply other threads:[~1995-09-08 4:00 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
1995-09-06 4:00 C compiler problem Basim Kadhim
1995-09-08 4:00 ` Basim Kadhim
1995-09-09 4:00 ` John Hascall
1995-09-08 4:00 ` Steve Emmerson [this message]
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox