-2

As printing will start from left side to right side in cout function, why these types of commands printing differently? please explain me. According to my knowledge the output of the following program should be 113 but it is 322. How?

#include<iostream.h>
void main()
{
int i=1;
cout<<i<<i++<<++i;
}

Output:

322

Thanks in Advance.

4

1 回答 1

0

您似乎假设增量从左到右发生,但根据标准,子表达式的评估顺序是unspecified。有关更多详细信息,请参见此处

在这种情况下,编译器似乎选择从右到左进行评估:

  • ++i发生,所以i == 22是表达式的值;
  • i++发生,递增i3但评估到它的旧值,2;
  • i现在被评估为3
于 2014-10-28T10:57:12.547 回答