Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!uakari.primate.wisc.edu!pikes!aspen.craycos.com!pmk From: pmk@craycos.com (Peter Klausler) Newsgroups: comp.lang.c Subject: Re: () ignored in some expressions Message-ID: <1990Apr10.235144.17925@craycos.com> Date: 10 Apr 90 23:51:44 GMT Organization: Cray Computer Corporation Lines: 21 > 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.