+ 1

int i=20; cout<<i<<i++<<++i

why is the output 22 21 21

3rd Apr 2018, 7:40 AM
Hari
4 Respostas
+ 18
In SL Answer will be 202022 it will depend upon compiler... https://code.sololearn.com/csddmCpnjkqf/?ref=app
3rd Apr 2018, 7:46 AM
🌛DT🌜
🌛DT🌜 - avatar
+ 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
3rd Apr 2018, 8:14 AM
storm
storm - avatar
+ 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.
3rd Apr 2018, 8:09 AM
Alex
Alex - avatar
+ 1
https://www.jdoodle.com/online-compiler-c++ Test the code here. 3 compilers available, 2 different results.
3rd Apr 2018, 8:49 AM
Alex
Alex - avatar