-7

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?

AmirRazoR
  • 146
  • 1
  • 9
  • 4
    This ones must have been asked a million times. – dandan78 Jan 25 '13 at 08:23
  • 1
    what have you tried? watch a kazillion answers show up ... and this is a dup's dup's dup! – thang Jan 25 '13 at 08:23
  • Nothing is wrong, except that you invoke side effects you do not understand. Don't use the ++ and -- operator until you do. – Ingo Jan 25 '13 at 08:26
  • i don't see how i could search to find an exact answer for what i was looking for. – AmirRazoR Jan 25 '13 at 08:47
  • @dandan78: If it has do you have a reference so we can link to them. Closing as too localized is wrong (as it could be usefull to a lot of people). But if it has been asked before we should link it as a duplicate. – Martin York Jan 25 '13 at 12:11
  • @loki I didn't vote to close. At any rate, the answers provide sufficient information for anybody who might stumble onto this question. – dandan78 Jan 25 '13 at 12:21
  • @dandan78: But your comment (without any evidence) has inspired others to vote to close (without actually doing it correctly). In the future if you declare its a dup please provide a reference to stop idiots impulse voting to close. – Martin York Jan 25 '13 at 12:25
  • i agree that my question should have been closed, but because of so many downvotes, now they banned my account.. – AmirRazoR Feb 25 '13 at 13:53

4 Answers4

4

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.

Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87
2
cout << x++ << endl;

Post-increment increments x from 0 to 1, and yields the old value, 0. The value of xis now 1.

cout << ++x << endl;

Pre-increment increments x from 1 to 2, and yields the new value, 2. The value of xis now 2.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

x++ will makes your variable 1 but is couted as 0 because of after++ behaviour by that time ++x will be 2

Post-increment and pre-increment within a 'for' loop produce same output

Community
  • 1
  • 1
Alexandru Calin
  • 220
  • 2
  • 11
1

X++ first prints it and then increments the value. ++X first increments the value and then prints it.

so,

cout<<x++<<endl   // Prints '0' and increments to '1'
cout<<++x<<endl   // Increments '1' to '2' and prints it
Ravi Shenoy
  • 467
  • 1
  • 4
  • 14