I'm new to C++ and study the increment and decrement operators. So I tried this example:
int x = 4;
cout << ++x << " " << x++ << " " << x++ << endl << endl;
cout << x << endl;
It returns this weird output on C++ .NET and QtCreator and 5 online C++ compilers:
7 5 4
7
The weird thing is that I expect something like this:
5 5 6
7
Can you explain what happens?
Please notice that cout << x++ << ++x;
is just another notation for:
operator<<( operator<< (cout, x++), ++x);
The order in which your x++ and ++x statements are evaluated is undefined, so is the effect of your code.
Even if it seems to happens from right to left in your particular examples, you should not rely on that by any means.
Just make another experiment by using multiple statements as:
cout << ++x << " ";
cout << x++ << " ";
cout << x++ << endl;
The solution to your problem is:
Never write code which results in undefined behaviour! :)