[net.lang.c] order-of-evaluation

fostel (02/28/83)

    The example on page 50 does not really answer the question posed along
    with the example "a[++i]=++i;". At least not in a pleasant way. The
    assignment statement itself is an expression, with the rhand and lhand
    sides simply sub-expressions. In the case of "a[i]=++i", the lhand side
    is not quite sufficently complex to illustrate the problem that was
    asked about.  Perhaps a better way to phrase the question is this:

        Will the expression evaluation order ever be something other then
        left-depth first, or right-depth first, or breadth firsr, or ....

    In other words, can one make any assumptions such as, once it has started
    on the lhand side will it go all the way there before starting on the
    rhand side?  The example on p 50 is not compelling since once it has
    started on the lhand side, it is effectively finished there. Adding
    the additional level of evaluation makes it a bit more difficult to
    see the example as a definitive answer.  BUT ...

    Ignore the example on p50 and look at the words. Don't assume anything
    about the order of execution. An optimizer will commonly reshuffle such
    things even if there is some higher level model such as left-depth-first,
    or whatever.  ANYWAY, NEVER WRITE UCKY CODE LIKE THAT -- it gives c a
    bad name.
    ----GaryFostel----
                      decvax!duke!mcnc!ncus!fostel

wagner (03/06/83)

To add more fuel to the fire, I think the example of 
something = foo(i++)
MUST pass the incremented value of i to foo; the standard
allows you flexibility of evaluation, but not the option of
avoiding evaluation of expressions passed to functions.
If foo can see another copy of i (perhaps it is global), the
two versions may differ, since you dont know that i was stored
back into until the end of the statement.

Michael Wagner, UTCS

wagner (03/07/83)

It was pointed out to me that my recent comments about the
expression
something=foo(i++)
were wrong.  I claimed that the language had to increment i before
passing it to foo.  That is nonsense.  I meant to type
something=foo(++i)
in which case (I think) my comments were correct.  Thanks to
dmmartindale from waterloo for pointing out my error.

Michael Wagner, UTCS