[Solved] Why 2 and -2 instead of 1 and -1? [closed]


You need to understand the concepts of post increment(decrement) and pre increment(decrement).

Post increment

cout << x++<<endl;

You can understand this line as “Return the value of x” + “increment the value of x”. I.e The return value is before the increment.

So return 0 and increase the value of x to 1.

Pre increment

cout<<++x<<endl;

This is the opposite – the incremented value is returned.

So increase the value of x to 2 and return 2.

0

solved Why 2 and -2 instead of 1 and -1? [closed]