0

How to solve this problem?

Int a=5; int b; b= a++ + ++a; what is the value of b?

14th Apr 2018, 12:11 PM
Kavya
Kavya - avatar
3 Réponses
+ 2
this is actually an undefined behavior, not even defined in the latest C++ standard, so it will depend on the implementation if and how it works
14th Apr 2018, 1:11 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 2
True. That's why it's best to write good code. Good code is more likely to be future proof, if weird hacks are avoided. It's an interesting question though.
14th Apr 2018, 1:16 PM
Emma
+ 1
It does the a+a first, then does the ++ twice: i.e. a+a = 10, then the two increments are performed, giving the answer of 12: Source: http://www.cplusplus.com/forum/beginner/26383/ Code to demonstrate: https://code.sololearn.com/c4tCJ8KvqIIj/#c
14th Apr 2018, 12:25 PM
Emma