That's because of the way post-increment works.. it's implemneted something like this:
int operator++(int a) //post increment (i++)
{
int tmp = a ; //where "a" is the variable you're incrementing
a = a + 1 ;
return tmp ;
}
int operator++(void) //pre increment (++i)
{
//this is only an example..
//where "this" is the pointer to the variable you're incrementing
*this = *this + 1 ;
return *this ;
}
int main()
{
int i = 10 ;
i = i++ ;
//when post increment operator is called it increments i
//but >>returns the original<< value viz 10.
//AFTER the increment assignment happens, so i is reset to 10.
printf("%d\n", i) ; //will print 10
i = ++i ;
//when pre increment operator is called it increments i
//and >>returns the incremented<< value viz 11.
printf("%d\n", i) ; //will print 11
return 0 ;
} thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75