Skip to main content
gamedev.net
🔒 Locked

a = --(++a)...

Started by Andrew Nguyen Jan 19, 2002 at 3:24 PM 9 replies 400+ views
Original Post
Andrew Nguyen
Andrew Nguyen
Try this:
  
#include <iostream.h>

int main()
{
      int a;
      int h;
      a = 10;
      h = (++a)--;
      cout << h;
      system("PAUSE");
      return 0;
}
  
Now, h outputs: 11 How is this? Well, I''m not sure. It is the same as h = a+1, but how does the above work? I expected h to equal 10! Is ANSI trying to destroy us? Should we conform to a new standard? No, it is a C++ idiom! I now no one will use this, but just pointing it out.
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
pizza box
pizza box
I believe h will be 11. In the statement

h = (++a)--;

a is incremented one before it is assigned to h, then after the assignment is done it is brought back down to 10. This would be an example of the main differences between post incrementing/decrementing and pre incrementing/decrementing.
krez
krez
dude, your compiler is fine...

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Kylotan
Kylotan
You know the difference between prefix and postfix, yes?
quote:
Original post by Andrew Nguyen

    
a = 10;
h = (++a)--;
cout << h;

The ++a is evaluated before a is returned. So first, a is incremented to 11. Now a is being modified by the postfix operator. The postfix operator returns its value first (currently 11) and Then decrements the value (down to 11). So h should always be 11, as far as I can see.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
Andrew Nguyen
Andrew Nguyen
I knew all of that, look at my last sentence.
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
Brobanx
Brobanx
Dude, (Andrew Nguyen) quit complaining about the "C++ idiosyncrasies" and just either LEARN TO USE THEM, or DON''T BUG THE REST OF US ABOUT THEM. I''m sure everyone else knows all about this too. If you plan on "comforming to a new standard", go ahead and do that, nobody likes a complainer.
Oluseyi
Oluseyi
quote:
Original post by Andrew Nguyen
I knew all of that, look at my last sentence.

Liar! Look at your last few instead:
quote:
How is this? Well, I''m not sure.
It is the same as h = a+1,
but how does the above work? I expected h to equal 10!
Is ANSI trying to destroy us? Should we conform to a new standard?

No, it is a C++ idiom!
I now no one will use this, but just pointing it out.

This is a beginner''s question. Ergo, you are an ignorant beginner who tries to pass for more knowledgeable than he is.

You''ve been blacklisted by me.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Andrew Nguyen
Andrew Nguyen
Walk down from you're ivory tower and read it again with more 'emotion'. You would see that some of the post was sarcastic. (especially the penultimate paragraph). I was just stating it, and I was about to make this part of another thread, but I felt that a topic would be more appropriate. I'm sorry if I'm not as stiff.

quote:

How is this? Well, I'm not sure.
It is the same as h = a+1,
but how does the above work? I expected h to equal 10!
Is ANSI trying to destroy us? Should we conform to a new standard



Well, read THIS again with a tad of sarcasm. Also, not to sound like a larger asshole, read the punctuation on the neaxt paragraph for god's sake!

Edited by - Andrew Nguyen on January 19, 2002 10:28:19 PM
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
krez
krez
people might think you are less of a dildo if you stopped saying stupid things instead of saying them and then later claiming they were a joke (by the way, get a dictionary and look up "sarcasm")...

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Andrew Nguyen
Andrew Nguyen
Can it be said all my posts are jokes? No, FLAME

How about serious... FLAME

Ask a question... FLAME

Make a statement... FLAME

Wondering Why People Didn''t Read the Whole Post... FLAME

I need a large bucket...
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.