-3

Possible Duplicate:
Order of evaluation of arguments using std::cout

I have known it now!This is responsible for 'cout' The all code:

#include <iostream>
using namespace std;
int main()
{
   int i = 3;
   cout <<-i++<<endl<<i<<endl<<-(i++)<<endl<<i<<endl;
   return 0;
}

I use VC++6.0 to compile,output is : -4 4 -3 3 But I use g++ to compile,output is : -4 5 -3 5 Why? I think they should be the same : -4 4 -4 4

PS:I try it:

int main()
{
   int i = 3;
   cout <<-i++<<endl;
   return 0;
}

And

int main()
{
   int i = 3;
   cout <<-(i++)<<endl;
   return 0;
}

I compile they one by one,the result is the same:-3 3 Thinks to all answers,I maybe have a mistake of testing -i++ and -(i++) somehow

Community
  • 1
  • 1
GrinV
  • 27
  • 5
  • could you reformat so we can see the full code. – Tom Tanner Dec 26 '12 at 15:30
  • 1
    It comes down to the order of evaluation. Have a look at this: http://stackoverflow.com/questions/3181211/prefix-postfix-increment-operators – Peter Dec 26 '12 at 15:30
  • I am a greenhand of C++,I haven't learned class – GrinV Dec 26 '12 at 15:36
  • 2
    @GrinV A useful [like to you answer](http://www.stroustrup.com/bs_faq2.html#evaluation-order) – Grijesh Chauhan Dec 26 '12 at 15:39
  • Thinks to your link,it is so detail that I must spend long time reading ,because I am Chinese,my English is poor – GrinV Dec 26 '12 at 15:42
  • The real problem of your code is that the order of evaluation of each expression is unspecified, so it may print different stuff depending on the mood of the compiler; read [this question](http://stackoverflow.com/questions/7718508/order-of-evaluation-of-arguments-using-stdcout) – Matteo Italia Dec 26 '12 at 15:44
  • There is no way that the code you have shown can result in either output. Cut and paste the code you're actually running. – John Bode Dec 26 '12 at 15:46
  • 1
    @GrinV: While each of your output statements depends on the order of evaluation, the actual results you reported are unrealistic. The code you posted cannot possibly produce the output you posted. Post the exact code that you ran and the exact output that it printed. – AnT stands with Russia Dec 26 '12 at 15:46
  • So,how can I master i++,I so confuse – GrinV Dec 26 '12 at 16:16

2 Answers2

6

As it stands right now, your code has undefined behavior. You're both using the value of i and modifying i without an intervening sequence point1. This gives undefined behavior.

The difference in results between the two lines does not stem from the use of parentheses -- it stems purely from the fact that the ++ in the first line modifies the value of i, so when you reach the second line, its value has been incremented.

To summarize, since you have undefined behavior, any output is perfectly reasonable and allowed, but the -4 4 -3 3 is quite reasonable and understandable.


  1. As of C++ 11, the standard no longer uses the phrase "sequence point", but uses phrasing like "sequenced before" or "sequenced after" to describe the same basic intent. A few things ended up getting defined behavior from this change, but not many (and, specifically, not this code).
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

The two lines are the same

Try

int i = 3;
cout << - i++  << endl << i << endl;
i=3
cout << -(i++) << endl << i << endl;

The operator precedence for any operator is found here:

http://en.cppreference.com/w/cpp/language/operator_precedence

And, postfix ++ (the one you use) has higher precedence than Unary plus and minus.

On the other hand, you should read this Q and A:

Why is a = i + i++ undefined and not unspecified behaviour

Your examples are actually identical to this:

(
  (
    (
      cout.operator<<(-(i++))
    ).operator<<(endl)
  ).operator<<(i)
).operator<<(endl);

In essence, modifying a variable and using it in the same expression is often undefined behaviour: the program is allowed to do anything.

Community
  • 1
  • 1
Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
  • 1
    Interestingly, since the order of evaluation is unspecified, despite being the same the two lines may produce different output. – Matteo Italia Dec 26 '12 at 15:51
  • Is that true? Do the *overloaded* `<<` and `>>` operators not introduce a sequence point? – John Bode Dec 26 '12 at 16:06
  • Thinks,I have to sleep now,I will read it later – GrinV Dec 26 '12 at 16:24
  • Yes,you are right ,think you – GrinV Dec 26 '12 at 16:44
  • @MatteoItalia, I think "Hello World" is an allowed output also... – Johan Lundberg Dec 26 '12 at 16:50
  • @JohanLundberg: I'm not entirely sure, IIRC the order of evaluation is simply *unspecified*, so it's not *undefined behavior* (where even nuclear war would be an allowed output). – Matteo Italia Dec 26 '12 at 16:52
  • @JohanLundberg: ok, I checked; §1.9 ¶15 says «If a side effect on a scalar object is unsequenced relative to [...] a value computation using the value of the same scalar object, the behavior is undefined», so you are right, it's UB. – Matteo Italia Dec 26 '12 at 16:57