+ 1
int i=20; cout<<i<<i++<<++i
why is the output 22 21 21
4 Respostas
+ 18
In SL Answer will be 202022
it will depend upon compiler...
https://code.sololearn.com/csddmCpnjkqf/?ref=app
+ 2
i=20
it will first output 20
i++ : one would expect to increment i, and actually it does increment, but only after it writes down i (because the increment operator comes afterwards i++). therefore it writes again 20. and then increments it to 21.
++i : this time the increment takes place before output. so 21 becomes 22.
final output:
20 20 22
and as there is no space between the outputs:
202022
+ 1
Undefined behaviour.
G++ 6.3.0 outputs 22 21 22
Clang 5.0.0 outputs 20 20 21
And whatever Compiler Hari used outputs 22 21 21.
So out of 3 compilers we get 3 different outputs.
And the moral of this story:
Don't use multiple increment or decrement operators in the same sequences.
+ 1
https://www.jdoodle.com/online-compiler-c++
Test the code here. 3 compilers available, 2 different results.