void main()
{
int i=1;
printf("%d,%d,%d",i++,i++,i++);
}

output

         3,2,1

CAN SOMEONRE EXPLAIN ME WHY?

Edited 3 Years Ago by pyTony: fixed formating

Although it seems pretty clear that the compiler you are using follows the normal "_cdecl" rules; it pushes parameters from right to left onto the stack, so it pushes the rightmost i, increments i, then the next i from the right, increments i, and then the next i from the left.

In __cdecl functions, arguments are pushed right to left and the caller is responsible for cleaning up the stack.

Compilers often implement other calling conventions (like __stdcall), but the _cdecl rules are always used on variable-argument routines like printf() because the callee does not know how many arguments there are.

Something to watch out for would be compiling with the optimizer turned ON vs OFF. With the optimizer turned on, the compiler might decide to, say, pass all three parameters as 1 and then add 3 to i. It is allowed to do this because of the "undefined behavior" Dave mentions.

consider the statement :
printf("%d%d%d",i++,i++,i++);
(p) (q) (r)
let the right most i++ be r and....
first of all the processeing in the above printf statement starts from right(ie r)
now i++ means post increment.
now r carries the value 1 but increments the value and gives it to q(ie 2).
now q carries the value 2 but increments the value and gives it to p(ie 3)
now p has the value of 3.
------------------------------------------
if i am wrong please correct me.thank you. :p
------------------------------------------

if i am wrong please correct me.thank you.

The correct answer is that there is no correct answer (which is pretty much what it said in the link I previously posted).

You are attempting to define undefined behavior. The particular implementation you have may behave just the way you described, but it in no way necessarily applies to any other implementation.

int i=1;
printf("%d,%d,%d",++i,++i,++i);

above will give u 4,3,2 on windows VC++

The correct answer is that there is no correct answer (which is pretty much what it said in the link I previously posted).

You are attempting to define undefined behavior. The particular implementation you have may behave just the way you described, but it in no way necessarily applies to any other implementation.

Have you ever heard of a stack? And what the hell is with void main()?

Edited 3 Years Ago by happygeek: fixed formatting

int i=1;
printf("%d,%d,%d",++i,++i,++i);

above will give u 4,3,2 on windows VC++

printf(4) is NOT C++, quit using it.

>above will give u 4,3,2 on windows VC++
It doesn't matter, the behavior is undefined. Your results can vary not only between compilers, but between different versions of a single compiler. And don't bump old threads.

>printf(4) is NOT C++, quit using it.
printf is standard C++, as are all of the C89 standard library functions and types.

>printf(4) is NOT C++, quit using it.
printf is standard C++, as are all of the C89 standard library functions and types.

Blasphemous! May Dennis Ritchie have mercy on your souls.

the complier always read the line from right to left.sinc it uses its stack.so if u take the program.first the right most end 'i' will be compiled..and its a post increment so it increment after assingment oprerator.so the output is 1 ,thn reads the second right most one.here the value of i is 2..and it is post increment.hence the output will be as 2 and the value of i will be incremented by 1 ..which means 3..hence wen the compiler reads the last 'i' it gives the output as 3..

This article has been dead for over six months. Start a new discussion instead.