Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!think.com!mintaka!bloom-beacon!eru!hagbard!sunic!kuling!cia.docs.uu.se!andand From: andand@cia.docs.uu.se (Anders Andersson) Newsgroups: comp.lang.c Subject: Re: Short circuit evaluation/expression rearrangement (2nd summary) Message-ID: Date: 3 Jun 91 14:36:55 GMT References: <1991May27.113628.26880@ucthpx.uct.ac.za> <16283@smoke.brl.mil> Sender: news@kuling.UUCP Lines: 29 In <16283@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes: >In article <1991May27.113628.26880@ucthpx.uct.ac.za> gram@uctcs.uucp (Graham Wheeler) writes: >>From: worley@compass.COM >>... in "a + b + c", >>the first addition must be performed before the second, because it >>associates as "(a + b) + c", showing that the second addition has as >>an argument the result of the first. >The above is incorrect. A conforming implementation is obliged to >perform the operations so that it doesn't create an exception where >one would not have occurred in evaluating the expression strictly >according to the precedence implied by the grammar, but within >those constraints it may perform the additions in any order. To >force an order of evaluation (in Standard C), you must use parentheses. You are dead wrong. Parentheses will do you no good whatsoever. The right for compilers to exploit associtivity and commutativity of operators was *removed* in the ANSI standard. In the standard, (a + b) + c IS the same as a + b + c. What you DO know: The addition of a and b will be made before c is added to the result. What you DO NOT know: The order of evaluation of the expressions a, b and c. That is, in the expression x/y + z/w + p/q, you cannot tell in which order the divisions are performed, but you do know the order in which the additions are made. -Anders Andersson (andand @ cia.docs.uu.se)