# i++ and ++i problem

This topic is 4874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

simple code below: int i = 0; cout << i++ << i++ << i++ << endl; int j = 0; cout << ++j << ++j << ++j << endl; The output is 210 333 on VC2005 beta2 But on gcc 3.3.3 the output is 012 123 I'm a little confused. According to ANSI C++, which is the correct output?

##### Share on other sites
Both are correct, it can do either when using multiple ++i and i++'s in a single statement.
It's another one of those places where the standard leaves the ultimate behaviour to the compiler.

##### Share on other sites
If you really want details, look into 'sequence points.' They define exactly when you know what has and what has not taken place. Anything between two sequence points is implementation defined, so that the compiler is free to optimize if possible.

CM

##### Share on other sites
Yeah, don't do that.

Seriously, people who obfuscate the meaning of their code like that deserve all of the headaches that come with it.

##### Share on other sites
Quote:
 Original post by Inmate2993Yeah, don't do that.Seriously, people who obfuscate the meaning of their code like that deserve all of the headaches that come with it.

Frankly, I don't think it looks obfuscated at all. Intuitively, the gcc output makes the most sense. Top to bottom, left to right. When you're first learning, why would you expect a language to *not* have well defined orders?

CM

##### Share on other sites
Quote:
Original post by Conner McCloud
Quote:
 Original post by Inmate2993Yeah, don't do that.Seriously, people who obfuscate the meaning of their code like that deserve all of the headaches that come with it.

Frankly, I don't think it looks obfuscated at all. Intuitively, the gcc output makes the most sense. Top to bottom, left to right. When you're first learning, why would you expect a language to *not* have well defined orders?

CM

Yes, however any reputable C++ book will cover the fact that the order of evaluation of function parameters is unspecified except that they will be evaluated before the call to the function. None the less, if someone hasn't gotten to that part of the book, it is no wonder they might think such.

##### Share on other sites
left to right? I don't know. I'd be pushing them on a stack (maybe through function calls) rather than a queue and evaluating them that way.

That is something my teacher told us about in our C++ class. Hopefully you figured this out before going through hours of headaches.

##### Share on other sites
The order of evaluation isn't just implementation defined -- as soon as you alter a value more than one time between sequence points, the expression itself yields undefined behavior, which is actually much worse for you if you are actually using code such as that in your program. In other words,

cout << i++ << i++ << i++ << endl

could literally crash your system, do absolutely nothing, or start playing Doom III; even though in practice, it probably would not do something quite so extreme. So, even if the order of evaluation doesn't matter to you, your code still has completely undefined results when doing something like that which you showed.

##### Share on other sites
Quote:
 Original post by Polymorphic OOPcould literally crash your system, do absolutely nothing, or start playing Doom III

If that happened it would so kick ass, especially considering that I don't even own Doom III.

Hack, hack, hack, compile, run, doom3, "WTF!"?
Play, play, play...

##### Share on other sites
Quote:
 Original post by nprzleft to right? I don't know. I'd be pushing them on a stack (maybe through function calls) rather than a queue and evaluating them that way.

cout << i++ << i++ << i++ << endl
=
((((cout << i++) << i++) << i++) << endl)

That's how I looked at it until I was proven wrong. At which point I just decided it was easier just to avoid changing variables in cout statements then to figure out how things actually work. In retrospect, it was a good call [grin]

CM

1. 1
Rutin
31
2. 2
3. 3
4. 4
5. 5

• 13
• 16
• 11
• 10
• 14
• ### Who's Online (See full list)

There are no registered users currently online

×