| | |
i=i++
![]() |
•
•
Join Date: Jan 2007
Posts: 8
Reputation:
Solved Threads: 0
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
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
That's because of the way post-increment works.. it's implemneted something like this:
c Syntax (Toggle Plain Text)
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 ; }
![]() |
Other Threads in the C Forum
- Previous Thread: Help! Help!help! Help!!
- Next Thread: Help with efficient memory management
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






