Newsgroups: comp.sys.amiga.programmer Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!caen!maize.engin.umich.edu!milamber From: milamber@caen.engin.umich.edu (Daryl Cantrell) Subject: Re: Compiler code (was a flame fest) (now a lesser flame fest) Message-ID: <1991Apr23.160035.16727@engin.umich.edu> Sender: news@engin.umich.edu (CAEN Netnews) Organization: University of Michigan Engineering, Ann Arbor References: <91112.093750GHGAQZ4@cc1.kuleuven.ac.be> <1991Apr23.020319.7216@engin.umich.edu> <91113.092907GHGAQZ4@cc1.kuleuven.ac.be> Date: Tue, 23 Apr 1991 16:00:35 GMT In article <91113.092907GHGAQZ4@cc1.kuleuven.ac.be> writes: >>> >>>I think you are wrong. Look at the following results : >>>I have tried this with our mainframe compiler : >>> int n=4; >>> >>> (n--)*(n++) -> 12 >>>[...] >>> (n++)*(--n) -> 16 >>> >>> Jorrit Tyberghein >> >> You missed my point. I was saying that ++ and -- are unary operators which >>will be evaluated before the multiplication. You've pointed out the argument >>this stems from, which is that there's no way to know which side of the mult- >>iplication is evaluated (You're compiler does the left first..). > >There is something I don't understand about this. You say that the compiler >evaluates left first. But how do you interprete (--n)*(++n) ? If the compiler >would go from left to right he should first compute --n (or n=n-1). This is >the left side of the expression. And after that the compiler would evaluate >(++n) which is the right side of the expression. Obviously I'm thinking wrong >because this would give 12 instead of 16. What really happens here is that > (--n)*(++n) gets translated to : > --n > ++n > (n)*(n) (or something equivalent) >I've looked at the compiled code and this is indeed what is evaluated. >But why does (n++)*(n--) not evaluate to the equivalent : > (n)*(n) > n++ > n-- >I thought that ALL --x or ++x operators where to be evaluated BEFORE the >evaluation of the total expression happens (and this is presumably also >what happens) and that ALL x++ or x-- operators where to be evaluated >AFTER the evaluation of the total expression (and this is presumably >not true). This would seem more logical to me. > >By the way. Lattice C generates exactly the same results. > > > Jorrit Tyberghein This is the "problem" with C (if such you consider it). "Left to right" refers to the order OPERATIONS are evaluated. There is NO defined order for evaluating the OPERANDS. For instance, if you take "a + b + c", C guarantees that a + b will be computed, and then added to c. However, there is no guarantee as to whether a or b will be evaluated first. ++ and -- are unary operators, which are evaluated with other operators of the same precedence (unary -, for instance). the difference between ++x and x++ is whether x is incremented before or after evaluating x. So if you had: int main() { int x = 4; printf("%d",x * 3 + ++x); } the answer I get (Lattice 5.1) is 17, not 20 as you suggest. -- +---------------------------------------+----------------------------+ | // Daryl S. Cantrell | These opinions are | | |\\\ milamber@caen.engin.umich.edu | shared by all of // | | |// Evolution's over. We won. | Humanity. \X/ | +---------------------------------------+----------------------------+