0

I am using Dev C++ with TDM-GCC 4.9.2 32 Bit compiler. I was trying to find the order of evaluation of expressions in printf function for which i made the program:

    #include<stdio.h>
    int main(){
        int x=0;
        printf("%d %d",x++,++x);
        return 0;
    }

If the order of evaluation turns out to be left to right, then the output will be 0 2 and for right to left the output will be 1 1. But the output comes out to be 1 2. How is this possible? If I write:

    printf("%d %d",x++,x);

then the output is 0 1 which indicates that order of evaluation is right to left and instead if i write:

    printf("%d %d",x,x++);

then the output is 1 0 which indicates that order of evaluation is left to right. Please explain me why I am getting contradictory outputs.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 5
    *Undefined behavior* - The order of evaluation is not defined (see the reference page: http://en.cppreference.com/w/c/language/eval_order) – UnholySheep May 15 '17 at 12:24
  • 1
    What this actually is is a load of *undefined behaviour*. But plus one for this particular variant of the common question - you've spent a lot of time building the case, even if the answer is obvious once you know it. – Bathsheba May 15 '17 at 12:24
  • You might find this video useful: https://vimeo.com/68390478 - especially about 7m55s into the video :) – Jimbo May 15 '17 at 12:29

0 Answers0