Can anyone pls explain the output for this

int main()
{
int i=10;
printf("%d %d %d",i++,--i,i);
return 0;

according to me the o/p should be 9 9 10 however gcc compiler is giving o/p is 9 10 10

can anyone explain me this....

Refer to this similar thread
http://www.daniweb.com/software-development/c/threads/405549

i checked the post.
but i am not able to get it..

when i execute the code on turbo c++ i get what i expect, but i am getting some other answer on gcc compiler.. why is that?

int main()
{
int i=5;
printf("%d %d %d",i,i++,++i);
return 0;
}

turbo C compiler outputs 7 6 6
where as gcc is giving 7 6 7


please explain... i m losing my head :sweat:

The result is undefined. Hence different for different compilers. That is what is given in the previous post.

As DJSAN10 mentions this is undefined behavior. You will not get a clearer explanation than in the already mentioned thread: Narue's comment

It is not guaranteed that an increment or decrement is performed immediately after giving up the previous value and before any other part of the expression is evaluated. It is merely guaranteed that the update will be performed sometime before the expression is considered "finished".

So, using of increments or decrements 1 at a time gives accurate result. But if used collectively, the compiler itself choses the increment or decrement to be performed and gives you a messy result. :P

I think it would help you...i posted same reply in that post also.. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.