Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
Why this code is generating 8 as a result ?
#include <iostream>
using namespace std ;
void myFunction(int i)
{
i = i + 2 + ++i;
cout<<i<<endl;
}
void main ()
{
int i = 2;
myFunction(i);
cin>> i;
}
I think the result should be 7 not 8…I am using Visual Studio 2008
The order of evaluation of terms on the right hand side of this expression
is undefined. i.e. they can occur in any order. In this case the compiler has chosen to increment i first (++i, third term), before evaluating i (first term), which results in 3 + 2 + 3.