From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,512b17e0104b2ceb,start X-Google-Attributes: gidfee84,public From: kadhim@munge.cs.colorado.edu (Basim Kadhim) Subject: C compiler problem Date: 1995/09/06 Message-ID: #1/1 X-Deja-AN: 109652310 distribution: world organization: Dept. of Computer Science, University of Colorado, Boulder reply-to: kadhim@cs.colorado.edu newsgroups: comp.unix.osf.osf1 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))); } Compiling the program with cc and running yields the value 8, which quite obviously indicates that the compiler is somehow interleaving the computation of the function arguments. When compiled with the "-migrate" flag, the program yields 9 as one would expect. I went looking through the ANSI C standard and didn't find anything that expressly disallows this interleaving behavior, which suggests that strictly speaking it doesn't violate the standard, however the Harbison & Steele book on C has the following to say: The original description of C specified that subexpressions may be evaluated in any order and that the arguments to a function may be evaluated in any order. The matter of interleaving was not discussed, nor the question of whether rearranging may alter the implicit type conversions. We advise implementors to adhere rigidly to the restrictions outlined here (which actually are quite sensible and not terribly restrictive). A little earlier they state: When evaluating the actual arguments in a function call, the order in which the arguments are evaluated is not specified; but the program must behave as if it chose one argument, evaluated it fully, then chose another argument, evaluated it fully, and so on until all arguments were evaluated. It sure would be nice if the Alpha compiler adhered to this. -- Basim Kadhim kadhim@cs.colorado.edu University of Colorado, Boulder