Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!overload!dillon From: dillon@overload.Berkeley.CA.US (Matthew Dillon) Newsgroups: comp.sys.amiga.programmer Subject: Re: Compiler code (was a flame fest) (now a lesser flame fest) Message-ID: Date: 23 Apr 91 21:01:52 GMT References: <1991Apr17.180342.25312@engin.umich.edu> <91112.093750GHGAQZ4@cc1.kuleuven.ac.be> Organization: Not an Organization Lines: 64 In article <91112.093750GHGAQZ4@cc1.kuleuven.ac.be> GHGAQZ4@cc1.kuleuven.ac.be writes: >> Nope. ++ and -- are unary operators of the highest precedence. The only >>difference between ++x and x++ is the value they assume, the order of eval- >>uation follows standard C precedence.. > >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--) -> 20 > (--n)*(++n) -> 16 > (++n)*(--n) -> 16 > (--n)*(n++) -> 12 > (++n)*(n--) -> 20 > (n--)*(++n) -> 16 > (n++)*(--n) -> 16 > > Jorrit Tyberghein Obviously the compiler is going to do it a certain way, but that represents only the particular implementation of the compiler used. If you have an expression: exp1 * exp2 There is NO GUARENTEE on the order of evaluation... exp1 before exp2 or exp2 before exp1, no matter *what* exp1 and exp2 contains. Period. Never rely on side effects a particular compiler gives you. If you have an expression: exp1 + exp2 * exp3 There is still NO GUARENTEE which expression will be evaluated first.. it could be exp1, exp2, OR exp3. The ONLY guarentee is that exp2 will be multipled to exp3 and then added to exp1 ... exp1 might be evaluated before exp2 & exp3, or after, etc, etc, etc.... The ONLY three C operators that guarentee evaluation order are &&, ||, and the comma as an operator (when not used in a subroutine call, where it doesn't act as the comma operator). ++ and -- are NOT guarenteed to execute their operations before or after the entire expression is computed, they could be localized to the sub expression, globalized, or the result otherwise evaluated at ANY TIME and assigned back to the variable at any time after all within the context of the expression. Something like: ++a + ++a If a is 1, the result can be 2 + 2 or 2 + 3 or 3 + 2 and at the end 'a' can be either 3 or 4 depending on how the compiler was designed. DO NOT DEPEND ON ANY PARTICULAR SIDE EFFECT WITH ++ or -- ACTING ON THE SAME VARIABLE MORE THAN ONCE IN ANY EXPRESSION, EVER, OR YOUR CODE WILL BE UNPORTABLE EVEN ACROSS REVISIONS OF THE SAME COMPILER! -Matt -- Matthew Dillon dillon@Overload.Berkeley.CA.US 891 Regal Rd. uunet.uu.net!overload!dillon Berkeley, Ca. 94708 USA