0
#include<iostream>
using namespace std;

int main(){

    int numb = 2;
    numb = ++numb + ++numb;
    cout << numb;
}

Why is the output 8?

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • The output is 8 because the compiler is allowed to increment `numb` two times before using it in the assignment expression. Which doesn't mean that it has to. This behaviour is undefined in the C++ standard and you should never write such code. – freakish Sep 14 '20 at 08:35
  • It's undefined as the operands of `+` are unsequenced. `numb = ++numb << ++numb;` would be well-defined – M.M Sep 14 '20 at 09:42

0 Answers0