+ 5

a += ++a logic

Let's look at this code int a = 2; a += ++a; cout << a; I expect the output to be 5 but the real output is 6. I don't understand this. Doesn't it has same meaning as: a = a + ++a? (shouldn't the first 'a' be the original value? )

16th Oct 2019, 11:48 AM
CodingPro
CodingPro - avatar
2 ответов
+ 2
I think this happens because expression a+=b can be also defined as a=a+(b); Because this will make sense if we assume it as a=a+(++a) https://code.sololearn.com/cufOX0HNd9fY/?ref=app Or maybe ~ swim ~ is right it can be sequence point also
16th Oct 2019, 12:21 PM
Arsenic
Arsenic - avatar
0
In Java, result a=5 only... with clear expression evaluation is left to right associative.. Edit: Output is compiler specific in c/c++. You may get different outputs in different compilers.
16th Oct 2019, 3:11 PM
Jayakrishna 🇮🇳