I printed the above line. But I got the result as 65.How does I increment and print despite the fact that I is incremented at the second time??
int i=5;
printf("%d%d",i,i++);
I printed the above line. But I got the result as 65.How does I increment and print despite the fact that I is incremented at the second time??
int i=5;
printf("%d%d",i,i++);
Your printf
call produces undefined behavior. It is illegal to modify i
(in i++
) and at the same time perform an independent read of i
without an intervening sequence point.
Various "orders of evaluation" do not matter here. All attempts to explain the behavior of this code based on the "orders of evaluation" or what happens "before" and what happens "after" are absolutely incorrect. The behavior is simply undefined. End of story.
As far as the C language itself is concerned, this code can print "Kill all humans!"
, crash the program, format your hard drive or simply refuse to compile.
The ANSI C99 ISO/IEC 9899:1999 standard says
6.5.2.2 Function calls The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.
as you just discovered, the order of evaluation is unspecified. The compiler is free to evaluate the arguments in any order. (In your case, i++ is evaluated before i.)