0
c++ Very important
int z = 10; cout << ++z << z << z++ << z<<"\n"; why?result different.
2 odpowiedzi
+ 20
The order in which your z++ and ++z statements are evaluated is undefined, so is the effect of your code.
Discussion : [https://stackoverflow.com/questions/33445796/increment-and-decrement-with-cout-in-c]
Avoid doing the incremental/decremental operation "in a row" inside the cout. That causes undefined behavior.
Example: Compare two groups together. They both do the same operation but as you clearly see, former expresion's behavior is completely undefiend.
#include <iostream>
using namespace std;
int main()
{
int x = 1;
int y = 2;
cout << x++ << " " << ++x << " " << --y << " " << y++ << "\n\n";
x = 1;
y = 2;
cout << x++ << endl;
cout << ++x << endl;
cout << --y << endl;
cout << y++ << endl;
}
Output:
2 3 2 2
1
3
1
1
Live link : [http://cpp.sh/8xuaw]
+ 16
Just adding another related thread to the question:
https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points