Code: main() { int a=2; printf("%d %d %d",a,a++,++a); } for the above program igot the result as 4,3,3 but Code: main() { int a=2; printf("%d",a); printf("%d",a++); printf("%d",++a); } fetched me this result 2,2,4 may i know why? altafahmed2k4@yahoo.co.in
Welcome to G4EF. Regarding the code snippets that you post in the threads please do use bb code to format the code snippets. Because it calculates right to left. See the out put of the following code Code: main() { int a=2; printf("%d",++a); printf("%d",a++); printf("%d",a); }
C standard doesn't say anything regarding order in which arguments are evaluated. It could be right-to-left , left-to-right or maybe something else. Try to write a code which is independent of order in which arguments are evaluated. The OP's code has undefined behaviour.
Yes thats true and also its an unwritten fact that C compilers calculate right to left where as some programming language specially if I am correct its Fortran calculates it from left to right.
I have tested the same on 5 main compilers when I was in my college TurboC2, TurboC3, BorlandC, Microsoft Visual studio and gcc compiler but there is no rule for behaving such.
TurboC2, TurboC3--->Total Crap BorlandC, Microsoft Visual studio and gcc compiler--->Yaa right What should I assume about version numbers. And why are you defending your wrong statement? You can not assume any particular order. It varies from compiler to compiler. In short, code has undefined behaviour.That's it.
I am not defending the statement. I am just saying what I mentioned earlier its <<unwritten fact that C compilers calculate right to left>>. I knew what you are mentioning about the undefined behaviour and so I wrote it the unwritten fact. I never said it behaves in such a manner. isnt it. From 5.x, 6,7,8 and I dont remember which one it was.