-1
main()
{
    int a=10;
    printf("%d\t%d\t%d\t%d",++a,--a,a--,a++);
    getchar();
    return 0;
}

While running this program in Visual Studio 2012, I am getting the output: 10 10 11 10.

While running this program in Turbo C, I am getting the output: 10 9 11 10

The second output(Turbo C) seems correct from the point of view that parameters are scanned from right to left, evaluated and put onto stack.

But i am sure that the output from Visual Studio also can't be wrong. So, why this difference in output?

ash
  • 156
  • 3
  • 12
  • sequence points, undefined behavior. Already asked and answered. – crashmstr Oct 09 '14 at 18:40
  • if your code is written to _depend_ on argument order of evaluation, then you are invoking ***[undefined behavior](http://en.wikipedia.org/wiki/Undefined_behavior)*** – ryyker Oct 09 '14 at 18:44

1 Answers1

1

The order of evaluation of arguments is not defined, that is, the compiler can evaluate them in any order(according to the standard).

kraskevich
  • 18,368
  • 4
  • 33
  • 45
  • Then how come the Turbo C compiler is evaluating them in same order again and again and VS Compiler is evaluating them in same order again and again. They should evaluate the arguments in different order each time or sometimes the program is run. – ash Oct 09 '14 at 18:45
  • @Ashish undefined behavior is just undefined behavior. If the C spec stated that arguments lists shall be evaluated in a different order each time, and your implementation did that, then it would not be undefined behavior. Just a poorly written spec. As it is written, the order is simply not specified. Your implementation chooses to evaluate argument in the order it does. Another implementation could use a different order. – ryyker Oct 09 '14 at 18:50