Short circuit evaluation/expression rearrangement (2nd summary)

Alan P Barrett barrett at Daisy.EE.UND.AC.ZA
Fri May 31 01:12:11 AEST 1991


In article <16283 at smoke.brl.mil>,
gwyn at smoke.brl.mil (Doug Gwyn) writes:
> >... 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.

I thought that the compiler was always governed by the "as if" rule:
the compiler may do whatever it likes as long as a strictly conforming
program cannot tell the difference between what the programmer asked for
and what actually happened.  But what Doug says seems to contradict
that, so I must be wrong.

Please could somebody explain exactly when the "as if" rule does or
doesn't apply.  In particular, related to Doug's message, I have the
following two questions.

If evaluation of an expression strictly according to the precedence
implied by the grammar would cause an exception, is the compiler
permitted to evaluate the expression in some way that does not cause an
exception?

Do parentheses really and truly *force* an order of evaluation?

--apb
Alan Barrett, Dept. of Electronic Eng., Univ. of Natal, Durban, South Africa
RFC822: barrett at ee.und.ac.za             Bang: m2xenix!quagga!undeed!barrett



More information about the Comp.lang.c mailing list