C++ code what is the answer of y | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

C++ code what is the answer of y

int a=5,y; y=++a + a++ + ++a; cout<<y;

20th Sep 2017, 6:09 PM
Md Shakif
Md Shakif - avatar
12 Answers
+ 9
21 is perfect answer given by Netkos Ent and also compiler to
20th Sep 2017, 6:44 PM
Md Shakif
Md Shakif - avatar
+ 7
can you define me how?
20th Sep 2017, 6:17 PM
Md Shakif
Md Shakif - avatar
+ 7
thanks now i am satisfied.
20th Sep 2017, 6:34 PM
Md Shakif
Md Shakif - avatar
+ 3
21
20th Sep 2017, 6:14 PM
AgentSmith
+ 3
Sure. ++a and ++a make a 7. Add the one from a++. 7 + 7 + 7 = 21
20th Sep 2017, 6:32 PM
AgentSmith
+ 3
i was going to say 19 (6 +6+7) but i am wrong and i see why...
20th Sep 2017, 6:37 PM
Kostas Batz
Kostas Batz - avatar
+ 3
Not sure why Baptiste got downvotes since he is correct, it is undefined. GCC and VC++ will indeed print out 21 here but clang prints 20. Not sure where people keep learning this stuff but you should never use it again.
20th Sep 2017, 10:20 PM
Dennis
Dennis - avatar
+ 3
Thanks for your support @Dennis ! They keep learning that because SoloLearn challenge has questions like that. The answer is the way the SoloLearn compiler (GCC) defined it, not the way all compilers defined it.
21st Sep 2017, 6:56 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
such questions should not be used in challenges at all, or the answer is to be "undefined" and "compiler dependent".
21st Sep 2017, 7:12 AM
yuri
+ 2
It is compiler dependant, so neither @Netkos or @Kostas are right or wrong
20th Sep 2017, 7:10 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
No @Md Shakif, it is undefined behavior
20th Sep 2017, 6:19 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
0
what is the right answer?
27th Dec 2017, 3:12 PM
Md Arif
Md Arif - avatar