output of printf("%d %d %d",a++,a++,++a);

how can be the output of following code 7 6 8
int a=5;
printf("%d %d %d",a++,a++,++a);
You should not change the value of any variable more than once in a single command. The result is undefined. It's the same reason why this: ++a++; is unreliable and the result will change from compiler to compiler.
This is an error, similar to reading an array out of bounds: the compiler doesn't have to report it (although some compilers do), but the output can be anything at all, the program may crash too, or do whatever.

For example, I get the output of "5 6 8" with the IBM and Sun compilers, and "7 6 8" (after two compiler warnings) with GCC, and both are equally correct.

Here's a summary of C++ evaluation order and when it is undefined: http://en.cppreference.com/w/cpp/language/eval_order
Last edited on
What i have read in a book that order of evaluation of arguments of a function is not guaranteed . So out of three which one will be evaluated first is depend on compiler we are using. So i think the result will be undefined .
Topic archived. No new replies allowed.