-4

What is execution strategy that the C++ runtime adopts to produce the output 4545 when executing the below given code.

#include <iostream>
using namespace std;
int main()
{
    int a=5;
    cout     <<a++     <<++a     <<a--     <<--a;
}

I expected the output to be 5444

Please help me understand how the C++ runtime handles this code.

Destructor
  • 14,123
  • 11
  • 61
  • 126

1 Answers1

0

The C++ runtime handles the entire program however it likes, because your program has undefined behaviour.

Don't bundle up increment/decrement operations like this.

That's been stated on SO thousands of times!

By the way, the C++ runtime does not "execute" your program; this is not Java. Your CPU executes your program. The C++ runtime is just there to provide libraries and whatnot to support that execution.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055