Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!cbatt!ihnp4!qantel!lll-lcc!lll-crg!seismo!sundc!hadron!jsdy From: jsdy@hadron.UUCP (Joseph S. D. Yao) Newsgroups: net.lang.c Subject: Re: Expression sequencing query Message-ID: <580@hadron.UUCP> Date: Sat, 27-Sep-86 01:47:51 EDT Article-I.D.: hadron.580 Posted: Sat Sep 27 01:47:51 1986 Date-Received: Tue, 30-Sep-86 06:48:37 EDT References: <760@oakhill.UUCP> <111@titan.UUCP> Reply-To: jsdy@hadron.UUCP (Joseph S. D. Yao) Distribution: net Organization: Hadron, Inc., Fairfax, VA Lines: 51 Keywords: Bugs Summary: Undefined order of execution! In article <111@titan.UUCP> eectrsef@titan.UUCP (Sean Eric Fagan - SA User Serv.) writes: >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. plus other comments tsk-tsk'ing this, etc. X3J11 states that subexpressions may be evaluated in any [presumably valid-jsdy] order, even if they produce side effects. The order in which side effects take place is unspecified. Side effects only have to be complete at what X3J11 calls "sequence points." The comma operator i s (in my [old] version) a sequence point, and the standard seems to require that at each comma, the left operand be evaluated and then the right, and the result be the latter. The example bears this out; but it is not as complicated as the above. Addition is not called a sequence point. HOWEVER As has been said before, X3J11 has little or nothing to do with contemporary C compilers. It hasn't even been issued yet! Not in final form. (So if the salesman knocks on your door with an "ANSI C compiler," slam it!) The bible has been K&R, which says specifically in the sections on Precedence and Order of Evaluation (K&R 2.12, Ref. 7.) the first two sentences above, plus: "When side effects (...) takes [sic] place is left to the discretion of the compiler, ... "... writing code which depends on order of evaluation is a bad programming practice in any language." Guy & Steele, although slightly influenced by X3J11, echo: (7.11) "It is, of course, bad programming style to have two side effects on the same variable in the same expression, because the order of the side effects is not defined; but the all-too-clever programmer here has reasoned that the order of the side effects doesn't matter, ..." It looks like your C compiler decided to evaluate all the lefts of the commas first, then the rights. For what it's worth, this resembles what G&S call "interleaving", which they deprecate in the evaluation of function arguments. (But not explicitly in this case.) Also for what it's worth, I would have hoped that the compiler did what you thought. HOWEVER (again), I've learned that where the document doesn't explicitly specify some part of the language, the definition of the language (for all practical purposes) resides in the compiler. -- Joe Yao hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP} jsdy@hadron.COM (not yet domainised)