Short circuit evaluation/expression rearrangement (2nd summary)

Anders Andersson andand at cia.docs.uu.se
Tue Jun 4 00:36:55 AEST 1991


In <16283 at smoke.brl.mil> gwyn at smoke.brl.mil (Doug Gwyn) writes:

>In article <1991May27.113628.26880 at ucthpx.uct.ac.za> gram at uctcs.uucp (Graham Wheeler) writes:
>>From: worley at 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)



More information about the Comp.lang.c mailing list