From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,512b17e0104b2ceb X-Google-Attributes: gidfee84,public From: steve@unidata.ucar.edu (Steve Emmerson) Subject: Re: C compiler problem Date: 1995/09/08 Message-ID: #1/1 X-Deja-AN: 109720068 references: organization: University Corporation for Atmospheric Research (UCAR) newsgroups: comp.unix.osf.osf1 kadhim@munge.cs.colorado.edu (Basim Kadhim) writes: >The Alpha C compiler behaves quite strangely with comma expressions. >Consider the following example: >#include >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