This is the output from the Microsoft VS 2009 compiler with default
optimizations. the increment operator is only evaluated once in the
equation.. It's evaluated the second time after the compiler is done with
the math. And the output in both cases will be 12.  Because you surrounded
m++ in parens it will be evaluated before it's used. (with this particular
compiler). 





; 12   : v=(m++)+(++m); 

 

            mov      eax, DWORD PTR _m$[ebp] ; gets m from storage into a
useful register.

            add      eax, 1                                  ; adds one to
it

            mov      DWORD PTR _m$[ebp], eax ; put's it back in storage

            mov      ecx, DWORD PTR _m$[ebp] ; move it again into a useful
register

            add      ecx, DWORD PTR _m$[ebp] ; add storage value to the
register

            mov      DWORD PTR _v$[ebp], ecx  ; move the register into where
v is stored.

            mov      edx, DWORD PTR _m$[ebp] ; NOW increment again..

            add      edx, 1

            mov      DWORD PTR _m$[ebp], edx

 

; 11   : v=v+(m++)+(++m);  

 

            mov      eax, DWORD PTR _m$[ebp]

            add      eax, 1

            mov      DWORD PTR _m$[ebp], eax

            mov      ecx, DWORD PTR _v$[ebp] ; the only difference here is
getting v into a useful register and then adding the m storage twice..

            add      ecx, DWORD PTR _m$[ebp]

            add      ecx, DWORD PTR _m$[ebp]

            mov      DWORD PTR _v$[ebp], ecx

            mov      edx, DWORD PTR _m$[ebp]

            add      edx, 1

            mov      DWORD PTR _m$[ebp], edx

 

Michael

  _____  

 

From: [email protected] [mailto:[email protected]] On Behalf Of
Aliya Awais
Sent: Wednesday, May 27, 2009 5:07 AM
To: [email protected]
Subject: [c-prog] confusing output

 






hi ! can someone please tell me the difference between output of given two
expression in C++ programs , how expressions are evaluated  in these two
cases?

#include<iostream.h> 
void main() 
{  
int m=5,v=0,u=0; 
v=v+(m++)+(++m);  
cout<<v<<endl;    //output is 11 
} 

#include<iostream.h> 
void main() 
{  
int m=5,v=0,u=0; 
v=(m++)+(++m); 
cout<<v<<endl;    //output is 12 (why confusing) 
} 

what is difference between v=v+(m++)+(++m);  and v=(m++)+(++m);  .
 
thanks

[Non-text portions of this message have been removed]





[Non-text portions of this message have been removed]

Reply via email to