0

Why the output of next code is 2 1 2?

#include "iostream"
int main(int argc, const char *argv[])
{
  int i = 0;
  std::cout << i << std::endl << i++ << std::endl << ++i << std::endl;
  return 0;
}

Because first i is equal 2 but not zero, it means that the whole like of cout is evaluated first and then printed (not part by part). If so, then first value should be 1, but not 2, because i++ should increment i after printing. Could you clarify?

EDIT:

The output of next code is 2 2 0.

#include "iostream"                                                             
int main(int argc, const char *argv[])
{
  int i = 0;
  std::cout << i << std::endl << ++i << std::endl << i++ << std::endl;
  return 0;
}

why?

ashim
  • 24,380
  • 29
  • 72
  • 96
  • See my answer, it is the same raeson: i = 0. Post incrementation, i is copied and then incremented. it is 1, then ++i, i = 2, then i, still 2. The value of i is 2 expect the first one that has been copied due to post incrementation – creack May 27 '13 at 00:20

3 Answers3

3

There is no sense reasoning in the output of your code because as it stands your program exhibits Undefined Behavior.

Per paragraph 1.9/15 of the C++11 Standard:

The value computations of the operands of an operator are sequenced before the value computation of the result of the operator. If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.

Because there is no sequence point separating both mutations of i, Undefined Behavior ensues. Your compiler might not output anything, and the program might output differently on different compilers. But arguing about the output is unnecessary in this context.


If you separate the statements, the result will then come out as expected:

std::cout << i   << std::endl;  // 0
std::cout << i++ << std::endl;  // 0
std::cout << ++i << std::endl;  // 2
David G
  • 94,763
  • 41
  • 167
  • 253
-2

the evalution goes from right to left.

i = 0

++i -> i = 1

i++ -> i = 1, post incrementation, a copy occurs. then i = 2

i -> i = 2

As all this occurs before being send to cout, the value of i is 2, and the middle one have been copied and its value is 1.

creack
  • 116,210
  • 12
  • 97
  • 73
-2

Please tell me if I don't understand your question clearly:

cout << i++;

is the equivalent of

cout << i;
i+=1;

while cout << ++i

is the equivalent of

i += 1;
cout << i; 

in otherwords any time you use i++, post-increment it returns the current value then changes while ++i means increment first then return the new value. It has nothing to do with cout

creack
  • 116,210
  • 12
  • 97
  • 73
Alex
  • 1,388
  • 1
  • 10
  • 19
  • I didn't downvote, but your answer's wrong, please see [my answer](http://stackoverflow.com/a/16764910/701092). – David G May 27 '13 at 00:33
  • Your answer was downvoted probably because you failed to address the question. That is, unless you can explain how your equivalency demonstrations can be expanded to apply to the OP's example: `std::cout << i << std::endl << i++ << std::endl << ++i << std::endl;` – Benjamin Lindley May 27 '13 at 00:33