+ 2

Help me explain b= a-- + --a and b= --a + a--

Hi guys, can you explain that cases for me? 1. int main() { int a= -5; int c = --a + a--; cout<<a; } // I dont understand why the answer is c = -13. I think it must be c = -12. 2. int main() { int a= -5; int c = a-- + --a; cout<<a; } //The answer is c = -12. I don't understand! Please help me.

11th Jul 2017, 4:25 AM
Minh
7 RĂ©ponses
+ 2
Are you sure that the last row in both examples Is "cout << a" not "cout << c"? If it "cout << c" than the first example: (-5-1) + (-6) = -12 --> Also don't know why -13 but I think that you have mistyped example or just I don't know... And second: (-5) + (-6-1) = -12
11th Jul 2017, 4:38 AM
Ondƙej Doněk
Ondƙej Doněk - avatar
+ 10
The behaviour of programs with multiple prefix/postfix operators between two sequence points is undefined. https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points
11th Jul 2017, 4:50 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
https://www.sololearn.com/discuss/523078/?ref=app
11th Jul 2017, 5:46 AM
‎ ‏‏‎Anonymous Guy
+ 1
@hatsy is right answer completly depends on compiler
11th Jul 2017, 5:42 AM
‎ ‏‏‎Anonymous Guy
+ 1
@Ondrej Donek : Thank you. I have mistyped. It is " cout<<c; ". Sorry I can't write your name exactly!
11th Jul 2017, 6:23 AM
Minh
+ 1
@Minh - No problem with name - our Czech language is strange but beautiful! :) Have a nice day.
11th Jul 2017, 6:52 AM
Ondƙej Doněk
Ondƙej Doněk - avatar
0
Thank everyone.
11th Jul 2017, 7:14 AM
Minh