I wrote this very simple program:
#include <iostream>
using namespace std;
int main()
{
int x = 0;
cout << x++<<endl;
cout<<++x<<endl;
int y = 0;
cout<<y--<<endl;
cout<<--y<<endl;
return 0;
}
and this is what i got when i ran it:
0
2
0
-2
i use code::blocks for writing the program and my operating system is Ubuntu 12.10.
how should i fix my program so i will see 0 1 0 -1 instead?
You need to understand the concepts of post increment(decrement) and pre increment(decrement).
Post increment
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
This is the opposite – the incremented value is returned.
So increase the value of x to 2 and return 2.