i=i++

Reply

Join Date: Jan 2007
Posts: 8
Reputation: mailsteam is an unknown quantity at this point 
Solved Threads: 0
mailsteam mailsteam is offline Offline
Newbie Poster

i=i++

 
0
  #1
Apr 13th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: i=i++

 
0
  #2
Apr 13th, 2007
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. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: i=i++

 
0
  #3
Apr 13th, 2007
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).
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: i=i++

 
1
  #4
Apr 13th, 2007
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 14
Reputation: deng_cen is an unknown quantity at this point 
Solved Threads: 4
deng_cen deng_cen is offline Offline
Newbie Poster

Re: i=i++

 
0
  #5
Apr 13th, 2007
++i and i++ are all 11 in .net
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC