x++ and ++x great confusion

HI guys. Can do simple statements involving x++ and ++x in multiple lines, but this expression in single cout statement is giving very weird result.

1
2
  int i=5;
    cout<<i++<<-i<<++i<<-i<<i;///output is 6-67-57 

more than others, that '5' is very weird. could anyone explain what goes on in step by step basis?
You can't modify and read the same variable inside the same expression like that. The C++ standard just says this is undefined, which means anything could happen.
Last edited on
The order of evaluation is undefined:

http://en.cppreference.com/w/cpp/language/eval_order

So i++ / ++i / and what not may happen before any of the function calls in no specified order.
It's worse than that. You can't have more than one side effect changing a variable in one expression. The results are undefined.
Topic archived. No new replies allowed.