Order of Evaluation (plus ?'s)

Walter Murray walter at hpclwjm.HP.COM
Thu Nov 23 04:38:17 AEST 1989


Wm E Davidsen Jr writes:

> In article <12570034 at hpclwjm.HP.COM> walter at hpclwjm.HP.COM (Walter Murray) writes:

>| There are times when the parentheses aren't even needed.  ANSI C guarantees
>| that
>|    p = q + r + s;
>| will behave exactly the same as
>|    p = (q + r) + s;

>  Could you quote sections on that? I don't seem to find the part which
>says that expressions are evaluated left to right.

I thought I might get challenged on that!  First, let me emphasize
what I'm NOT saying.  Consider a more general example:

   double sum, x(), y(), z();
   sum = x() + y() + z();

Functions x, y, and z may have side effects.  I am not saying anything
about the order in which those side effects will occur.  The
actual call to z may occur before the calls to x and y.  What I am
saying is that, of the two additions in the expression, the addition
of x() and y() will be performed first, then z() will be added to
that result.

The implementation is free to rearrange things only if it can't make a
difference.  For floating-point arithmetic, of course, it CAN make a
difference.

This is covered in an example in 2.1.2.3.  If you have the December 7,
1988, edition of the dpANS, see page 9, line 46, through page 10, line 21.

I realize that examples are not part of the Standard, but they do
attempt to convey what the Committee members intended for the Standard
to say.  To prove my original assertion from the Standard itself, the
best I can do is to cite the syntax for additive operators (3.3.6),
which defines '+' as a left-associative operator, and then cite
the third paragraph of 3.3, which implies that the syntax can
specify order of evaluation.

Maybe someone else can present a stronger argument in support of
the example I am referring to.

Walter Murray
walter at hpda.HP.COM
---



More information about the Comp.std.c mailing list