Quoting Allan Martinez Tabilog <[EMAIL PROTECTED]>: > Consider the following expression: > > a = ++b + ++b; > > a and b are integers initialized to zero. As I understand it, this is > how the expression is evaluated: first evaluate (++b) giving the value 1 > for b and for the expression (++b); evaluate the second (++b) giving the > value 2 for b and 2 for the second (++b) expression. Then a recieves the > value 1 + 2 or 3, right? But when I compiled a program to test this > expression using gcc I got the result that a = 4 (and b = 2) instead > of a = 3. Where did I go wrong in my understanding of this expression? I think your reading of the code is correct; I read it the same way too. However, gcc optimizes in default mode, and will replace common subexpressions (like the two occurences of ++b) by the latest value (2). You can see this in the generated assembly language code by compiling using the command line: gcc -S add.c -o add.s To force partial evaluations (not to replace common subexpressions) you can use the following construct: int a=0, b=0, c=0; a = (c = ++b) + ++b; The resulting values will then be c==1, b==2, a==3. I think it is also possible to use a compiler directive not to optimize. > Any tips on how to analyze this and similar expressions (e.g. b++ + b++, > ++b + b++, etc). Similar remarks hold for the post-increment operator. >>PMana ------------------------------------------------- This mail sent through IMP: mail.ateneo.net - Philippine Linux Users Group. Web site and archives at http://plug.linux.org.ph To leave: send "unsubscribe" in the body to [EMAIL PROTECTED]
