1

I wrote a simple program where int a = 10 the o/p which i was expecting was 10 9 9

 printf("a++:%d \t a:%d \t --a: %d \n", a++, a, --a); 

but the o/p which is got is 9 10 10

I wrote another printf like printf(" a:%d \t --a: %d \n", a, --a);

the o/p which i got is 9 9

can anyone explain me about this ?

Manu
  • 5,534
  • 6
  • 32
  • 42
  • are you sure that u get 9 10 10 in the first printf ? – Arif YILMAZ Apr 16 '13 at 09:36
  • Other possible duplicates: http://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c http://stackoverflow.com/questions/15027347/c-function-parameter-evaluation-and-passing http://stackoverflow.com/questions/9566187/function-parameter-evaluation-order – aib Apr 16 '13 at 09:38
  • "can anyone explain me about this ?" -- if you want to know what the compiler did, that produced that result, then either disassemble the executable or else tell your compiler to output assembly. But in a fairly important sense it doesn't matter. Your code has undefined behavior per the standard, so "legally" the compiler could do anything. It's also likely to vary by compiler, version and optimization options. – Steve Jessop Apr 16 '13 at 09:39
  • yes i am, u can also check once for your cofirmation – Manu Apr 16 '13 at 09:39
  • Another dupe: http://stackoverflow.com/questions/949433 – Steve Jessop Apr 16 '13 at 12:53

1 Answers1

3

You're experiencing undefined behavior, by having multiple expressions with side-effects, without sequence points inbetween.

There's no guarantee in which order function arguments are evaluated, so there's no way to "expect" something here (unless you wrote the compiler).

unwind
  • 391,730
  • 64
  • 469
  • 606