The below code gives following output:
5 6 3
6
can someone explain me how?Code:#include<stdio.h> void main() { int i = 3; printf ("\n%d %d %d\n", i++, ++i, i++); printf("%d\n", i); }
The below code gives following output:
5 6 3
6
can someone explain me how?Code:#include<stdio.h> void main() { int i = 3; printf ("\n%d %d %d\n", i++, ++i, i++); printf("%d\n", i); }
That is improper use of prefix and postfix operators on the same variable in the same expression. In other words, you are getting exactly what you are asking for: undefined behavior.
Mainframe assembler programmer by trade. C coder when I can.
If you want to do a bit more reading on this, check these links from the comp.lang.c FAQ:
http://c-faq.com/expr/seqpoints.html
http://c-faq.com/expr/evalorder4.html
http://c-faq.com/expr/experiment.html
http://c-faq.com/expr/evalorder2.html
http://c-faq.com/expr/ieqiplusplus.html
and so on..
Make sure you read the 'see also' references as well.
Thanks guys...that was very enlightening!