C ++中的operator ++ [英] operator ++ in C++
问题描述
请帮我解释一下。
我正在使用MS Visual C ++ 6.0。首先我进行初始化:int i = 5;
>如果我使用printf(%d%d%d,i ++,i ++,i ++);输出为5 5 5
>如果我使用cout<< i ++<<< i ++<< i ++;输出是7 6 5!
这太混乱了!由于后增量算子的残值定律,我认为输出必须是5 6 7!为什么7 6 5 - 相反的顺序?
而且,printf和cout是不同的,但我想知道为什么?
Please help me explain this .
I'' m using MS Visual C++6.0 . First i take the initialization :int i=5;
>if i use printf("%d %d %d",i++,i++,i++) ;the output is 5 5 5
>if i use cout<<i++<<i++<<i++; the output is 7 6 5 !
It''s so confusing ! Due to the residual -value law of the post increment operator i think the output must be 5 6 7 ! Why 7 6 5 - a reverse order ?
And more , printf and cout are different , but I wonder why ?
推荐答案
请帮我解释一下。
我正在使用MS Visual C ++ 6.0。首先我进行初始化:int i = 5;
>如果我使用printf(%d%d%d,i ++,i ++,i ++);输出为5 5 5
>如果我使用cout<< i ++<<< i ++<< i ++;输出是7 6 5!
这太混乱了!由于后增量算子的残值定律,我认为输出必须是5 6 7!为什么7 6 5 - 相反的顺序?
而且,printf和cout是不同的,但我想知道为什么?
Please help me explain this .
I'' m using MS Visual C++6.0 . First i take the initialization :int i=5;
>if i use printf("%d %d %d",i++,i++,i++) ;the output is 5 5 5
>if i use cout<<i++<<i++<<i++; the output is 7 6 5 !
It''s so confusing ! Due to the residual -value law of the post increment operator i think the output must be 5 6 7 ! Why 7 6 5 - a reverse order ?
And more , printf and cout are different , but I wonder why ?
我不能完全确定这一点,但我猜这是因为printf有括号 - 它被解释为单一声明,参数不是一次全部 - 它们被处理,因为它们被赋予cout,所以第一个完成,增加,然后第二个被采取,并在打印后,增加,等等。 ..
I''m not entirely sure on this, but I''d guess that it''s because printf has parentheses - it''s interpreted as a single statement, the arguments are not taken all at once - they''re dealt with as they are given to cout, so the first one is done, incremented, and then the second one is taken, and after printed, is incremented, etc...
实际上,我只是在Cygwin中运行它,它给了我(我初始化为1):
1 2 3
4 5 6
我认为这可能与VC ++编译器有关...
Actually, I just ran that in Cygwin, and it gave me (with i initialized as 1):
1 2 3
4 5 6
I think that might be something with the VC++ compiler...
实际上,我只是在Cygwin中运行它,它给了我(我初始化为1):
1 2 3
4 5 6
我认为这可能与VC ++编译器有关...
Actually, I just ran that in Cygwin, and it gave me (with i initialized as 1):
1 2 3
4 5 6
I think that might be something with the VC++ compiler...
是的,只是跑了它在Ubuntu上得到了
3 2 1(虽然我先做了cout) br />
4 5 6(这是printf行)
Yep, just ran it on Ubuntu and got
3 2 1 (I did cout first, though)
4 5 6 (and this was the printf line)
这篇关于C ++中的operator ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!