Re: 'C' Operators precedence

H. Peter Anvin (hpa@transmeta.com)
20 May 1998 02:39:20 GMT


Followup to: <Pine.LNX.3.95.980519212238.363A-100000@chaos.analogic.com>
By author: "Richard B. Johnson" <root@chaos.analogic.com>
In newsgroup: linux.dev.kernel
>
> When seeing:
>
> a = f(b) + g(c);
>
> ... the compiler is still not free to evaluate 'b' and 'c'
> in any order because of association, also called
> associativity, which requires operators of identical
> precedence to be evaluated left-to-right. This is why the
> functions (or macros) will be evaluated left-to-right.
>

Incorrect. Associativity just means that (for example):

a = b - c - d;

... must evaluate as:

a = (b - c) - d;

rather than:

a = b - (c - d);

However, for *ANY* of these expressions, the compiler is free to
evaluate b, c, or d in any order it darn well pleases; associativity
only controls how the results are to be combined.

> When seeing:
>
> a = b() + c();
>
> .... the compiler is told to evaluate nothing (void) using
> object 'b', then evaluate nothing using object 'c'.
>
> When seeing:
>
> a = b + c + d;
>
> ... the compiler may still not legally evaluate the objects
> in any order because of associativity, but many/most do.
> Optimization is not yet part of the 'C' standard. When
> attempting to optimize code, certain standards are broken as
> long as the code is likely to work.
>
> Everything makes sense once one understands the '()' operator.

This is just plain WRONG. The C standard is very explicit that there
are no sequence points between the evaluation of the arguments of
addition; in fact, only a few operators have any serialization
property; && and || does, and maybe (I am not sure about this one) the
assignment operators.

-hpa

-- 
    PGP: 2047/2A960705 BA 03 D3 2C 14 A8 A8 BD  1E DF FE 69 EE 35 BD 74
    See http://www.zytor.com/~hpa/ for web page and full PGP public key
        I am Bahá'í -- ask me about it or see http://www.bahai.org/
   "To love another person is to see the face of God." -- Les Misérables

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu