cout vs printf - 执行顺序 [英] cout vs printf -- order of execution
问题描述
可能重复:
cout<
-
这:
int k = 3;
printf(%d%d%d,k ++,k,++ k);
输出为
4 4 4
被推入堆栈中:%d%d%d
4 - 用于k ++
4 --for k
4 --for ++ k
右
-
这:
int k =
cout<< k ++<< k<< ++ k;
实际上是重复的函数调用,因此它等效于:
(((cout< k ++)<< k)<
所以,我想首先
k ++
那么k
然后++ k
必须始终按此顺序执行,对吗?我相信函数调用是一个序列点,但是输出在不同的实现上有所不同。为什么会这样呢?
点之间,在printf语句中。没有序列点,编译器可以自由地对内存位置 k
按顺序写入。
可能想知道什么是地狱点是一个seqeunce点,为什么它是相关的?基本上,序列点是代码中的一个点,其中所讨论的存储器位置,在这种情况下 k
已被修改最多一次。此处有更完整的说明: http:// www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.16
从FAQ中可以看出, $ , c>
operator>>
。函数调用引入了序列点,因此对内存位置 k
的修改具有定义的顺序。然而(这是我错过了一点,但Cubbi指出),因为C / C ++没有定义函数参数的评价顺序,这些参数,即使它们是函数,可以按照编译器定义的任何顺序进行评估。所以在表达式中: f(h(),g())
是否首先评估h()或g()是否未定义: http://www2.research.att.com/~bs/bs_faq2.html#evaluation-order 。所以这就是为什么即使在cout的情况下,你从不同的编译器得到不同的结果,原因是因为 cout < k ++<< k<< ++ k
转换为 cout.operator<<(k ++)。operator<<(k).operator(++ k)
实际上是这样的表达式: f(h(g(cout,k ++),k),++ k)
,并且每个函数参数以未指定的顺序计算。 / p>
Possible Duplicate:
Compilers and argument order of evaluation in C++
cout << order of call to functions it prints?
This:
int k=3; printf("%d %d %d",k++,k,++k);
Gives output as
4 4 4
because they are pushed into the stack as:%d%d%d 4 -- for k++ 4 --for k 4 --for ++k
Right?
This:
int k = 3; cout << k++ << k << ++k;
Is actually repeated function calls, so it's equivalent to:
( ( (cout << k++) << k) << ++k);
So, I suppose first of all
k++
thenk
and then++k
must always be executed in this order, right? I believe a function call is a sequence point, but the outputs vary on different implementations. Why is this so?
This is undefined because there is no sequence point between the , in the printf statement. Without a sequence point the compiler is free to order writes to the memory location k
as it wills.
Now you may be wondering 'what the hell is a seqeunce point' and why is it relevant? Basically a sequence point is a point in the code where the memory location in question, in this case k
has been modified at most once. There is a fuller description here: http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.16
As you can see from the FAQ, the ,
in the printf does not introduce a sequence point.
In the case of cout
this is different because there are 3 function calls to operator >>
. A function call introduces a sequence point therefore the modifications to memory location k
have a defined order. However (and this was a point I missed but Cubbi pointed out) because C/C++ doesn't define the order of evaluation of function arguments those arguments, even if they are functions, can be evaluated in any order the compiler defines. So in the expression:
f(h(), g())
Whether h() or g() is evaluated first is undefined: http://www2.research.att.com/~bs/bs_faq2.html#evaluation-order. So this is the reason why even in the case of cout you are getting different results from different compilers, basically because the cout << k++ << k << ++k
translates to cout.operator<<(k++).operator<<(k).operator(++k)
which is effectively an expression like this: f(h(g(cout, k++), k), ++k)
and each function argument is evaluated in an unspecified order.
这篇关于cout vs printf - 执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!