0

include

int main() {

using std::endl;
using std::cout;
int a=0;
cout << a++ << endl << ++a << endl;
int b=0;
cout << b++ << endl;
cout << ++b;

}

The output is 1 2 0 2. Why does a ++ output 1? Shouldn't it be 0? Can anyone tell me the principle!

Community
  • 1
  • 1
teng wang
  • 29
  • 3
  • 3
    It's very complicated because the rules on this have changed several times with different versions of C++. If you search you will find that this question has already been asked *hundreds* of times. Just don't write code like this. There's no point to it, that's all you need to know. – john Mar 16 '20 at 10:00
  • what compiler are you using? both g++ and clang++ produced 0 2 0 2 on my machine – Adham Zahran Mar 16 '20 at 10:01
  • I use colon and vs on my machine. – teng wang Mar 16 '20 at 10:40
  • I won't write code this way – teng wang Mar 16 '20 at 10:41

0 Answers0