944,134 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3363
  • C RSS
Apr 13th, 2007
0

i=i++

Expand Post »
Can somebody give me the reason why the integer i is not getting incremented.
Here is the code:

main()
{
int i=10;
i=i++;
printf("%d",i);
}

and the output is 10.

Here my doubt is that, i know that i is post incremented. Eventhough i is not incremented at the time of assignment, i should get incremented after that instruction(post incrementation) ie the printf statement should print the incremented value as in the below example

{
int i=j=10;
i=j++;
printf("%d %d",i,j);
}

here the output is 10 11. and i kno the reason that i gets the value before the increment and j is incremented(post incremented) after the completion of the instruction.
My question is y the same thing is not happening in the previous example

Waiting for your valuble explanations..
Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mailsteam is offline Offline
8 posts
since Jan 2007
Apr 13th, 2007
0

Re: i=i++

That's because of the way post-increment works.. it's implemneted something like this:

  1. int operator++(int a) //post increment (i++)
  2. {
  3. int tmp = a ; //where "a" is the variable you're incrementing
  4. a = a + 1 ;
  5. return tmp ;
  6. }
  7.  
  8. int operator++(void) //pre increment (++i)
  9. {
  10. //this is only an example..
  11. //where "this" is the pointer to the variable you're incrementing
  12. *this = *this + 1 ;
  13. return *this ;
  14. }
  15.  
  16. int main()
  17. {
  18. int i = 10 ;
  19. i = i++ ;
  20. //when post increment operator is called it increments i
  21. //but >>returns the original<< value viz 10.
  22. //AFTER the increment assignment happens, so i is reset to 10.
  23. printf("%d\n", i) ; //will print 10
  24.  
  25. i = ++i ;
  26. //when pre increment operator is called it increments i
  27. //and >>returns the incremented<< value viz 11.
  28. printf("%d\n", i) ; //will print 11
  29. return 0 ;
  30. }
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Apr 13th, 2007
0

Re: i=i++

When you use a variable twice in an expression and one of those instances is a pre- or post- increment, the behavior is undefined (and then depends on how it's implemented by your compiler).
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Apr 13th, 2007
1

Re: i=i++

Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Apr 13th, 2007
0

Re: i=i++

++i and i++ are all 11 in .net
Reputation Points: 30
Solved Threads: 4
Newbie Poster
deng_cen is offline Offline
14 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help! Help!help! Help!!
Next Thread in C Forum Timeline: Random Double Generation With Precision?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC