() ignored in some expressions

Peter Klausler pmk at craycos.com
Wed Apr 11 09:51:44 AEST 1990

> I often what to do things like increase an integer by 50%.  This can
> be done as follows:    i = (i*3)/2;   If an optimizing compiler can
> ignore the parenthesis, then it will probably notice that it can evaluate
> 3/2 at compile time to 1.  It will then notice that a multiplication by
> 1 can be ignored.  The end result is that my optimizing compiler will
> decide not to generate any code at all for the above statement.
> According to K&R, the only way to avoid this disaster is to code the
> operation in two steps:   i *= 3; i /= 2;

I doubt you've actually seen this behavior.

No working compiler is going to do this. Integer division must always be
treated as a special case, and rules of associativity must be followed.
Optimizations of C programs must yield code that functions "as if" naive
interpretation were done; and in C, arithmetic operators associate from
left to right.

Take a look at the code coming out of your compiler; more likely it's
computing (i+i<<1)>>1 or, better, (i+i>>1) for i*3/2. If it's discarding the
statement as null, it's broken.

More information about the Comp.lang.c mailing list