increment and decrement with cout in C++ -


this question has answer here:

i'm new c++ , study increment , decrement operators. tried example:

    int x = 4;         cout << ++x << "    " << x++ << "    " << x++ << endl << endl;     cout << x << endl; 

it returns weird output on c++ .net , qtcreator , 5 online c++ compilers:

7    5    4  7 

the weird thing expect this:

5    5    6  7 

can explain happens?

please notice cout << x++ << ++x; notation for:

operator<<( operator<< (cout, x++), ++x); 

the order in x++ , ++x statements evaluated undefined, effect of code.

even if seems happens right left in particular examples, should not rely on means.

just make experiment using multiple statements as:

cout << ++x << " "; cout << x++ << " "; cout << x++ << endl; 

the solution problem is:

never write code results in undefined behaviour! :)


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -