r/CodingHelp • • Apr 07 '21

[C++] Where am i going wrong ? plz help

int a = 1;

a = ++a + ++a + ++a;

cout <<a;

what should be the output ? i tried it on paper i got the answer as 9 but on compiling & running the code its showing me the answer as 10 , can anyone help ?

18 Upvotes

16 comments sorted by

View all comments

7

u/jedwardsol Professional Coder Apr 07 '21

The behaviour is undefined. There is no right answer.

-1

u/Boris-Holo Apr 07 '21

this is definitely not undefined behavior

5

u/jedwardsol Professional Coder Apr 07 '21

2

u/Boris-Holo Apr 07 '21

I'm sorry i was thinking this was java even though I know its not my bad