This post is in reference to a problem about which I posted earlier, under the title "multiple increment operators". Now while using multiple increment operators in C , the results are completely different, which also encouraged me to explain a little bit about the printf(); used in C for basic output operations.
Consider the following snippet of code :-
// Multiple increment opearators in C
#include <cstdio>
int main()
{
int x = 5;
printf("%d%d%d%d%d",x++,x--,++x,--x,x);
return 0;
}
The program give the output as 45545.
Now when the statement printf("%d%d%d%d%d",x++,x--,++x,--x,x); is executed, all the variables are processed from right to left , and pushed accordingly in the stack. i.e
x - PUSH --> means x = 5;
--x (per-decrement) - PUSH --> means x = 4;
++x (per-increment) - PUSH --> means x = 5;
For more info on using printf(), follow this link : http://stackoverflow.com/questions/2198880/how-does-printf-work
------ Related Posts ------
Multiple increment operators in c++ : http://programsplusplus.blogspot.in/2012/01/multiple-increment-operators-in-sinlge.html
Consider the following snippet of code :-
// Multiple increment opearators in C
#include <cstdio>
int main()
{
int x = 5;
printf("%d%d%d%d%d",x++,x--,++x,--x,x);
return 0;
}
The program give the output as 45545.
Now when the statement printf("%d%d%d%d%d",x++,x--,++x,--x,x); is executed, all the variables are processed from right to left , and pushed accordingly in the stack. i.e
x - PUSH --> means x = 5;
--x (per-decrement) - PUSH --> means x = 4;
++x (per-increment) - PUSH --> means x = 5;
x-- (post-decrement) - PUSH --> means x = 5 (but afterwards x = 4 due to post decrement)
x++(post-increment) - PUSH --> means x = 4; (but afterwards x = 5 due to post increment).
x++(post-increment) - PUSH --> means x = 4; (but afterwards x = 5 due to post increment).
Now the printf() statement , prints the values of x , hence the output is 45545(remember stacks follow LIFO,last in first out).
------ OUTPUT ------
------ OUTPUT ------
For more info on using printf(), follow this link : http://stackoverflow.com/questions/2198880/how-does-printf-work
------ Related Posts ------
Multiple increment operators in c++ : http://programsplusplus.blogspot.in/2012/01/multiple-increment-operators-in-sinlge.html
Sorry man ... but the answer is compiler dependent or undefined
ReplyDelete