What is the output of this program? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the output of this program?

#include <iostream> int main() { int a = 0; std::cout << (++--++a)++ + ++a; return 0; }

5th Jul 2016, 2:50 PM
Garme Kain
Garme Kain - avatar
3 Answers
+ 2
The output should be 4... Why? ++--++ is the same as ++ so you could rewrite your expresion as: (++a)++ + ++a... What happends then? the value in a is incremented two times before the addition leaving: (2)++ + 2 which is equal to 4... after that, a is incremented by one one more time leaving the value in a equal to 3... but as I said before, what you get on screen is 4 which is the sum of a before the last increment...
5th Jul 2016, 3:58 PM
Nelson Urbina
Nelson Urbina - avatar
0
But in this program the order of operations are well defined...
5th Jul 2016, 2:58 PM
Garme Kain
Garme Kain - avatar
0
Error
5th Jul 2016, 8:05 PM
Mihai Dancaescu
Mihai Dancaescu - avatar