0

Why are post-fix and pre-fix code and syntax in c++ not producing the intended output?

Why is the output of this code: https://code.sololearn.com/cSo0xc6skXfG/?ref=app #include<iostream> using namespace std; int main() { int x = 0; int y = 0; y = ++++x+x++; cout << y << endl; cout << x << endl; return 0; } 5 3 Shouldn't it be: 4 3 Please help!

12th Apr 2020, 9:36 AM
Mouhammed Nashat Helmi
4 Réponses
+ 3
((++(++x)) +(x++)) and also See the warning about sequence point.. Result is undefined by standards so Output is different in different compilers...... https://en.m.wikipedia.org/wiki/Sequence_point Edit: See in this, @swim given explanation for almost similar example ++a + ++a https://www.sololearn.com/Discuss/2038766/?ref=app
12th Apr 2020, 10:37 AM
Jayakrishna 🇮🇳
+ 1
i think because x++ itself will call ++x before returning rvalue, so the first x will increment 3 times
12th Apr 2020, 10:07 AM
durian
durian - avatar
+ 1
Thanks guys... I appreciate your help :)
12th Apr 2020, 11:23 AM
Mouhammed Nashat Helmi