COUT VS printf的 - 执行顺序 [英] cout vs printf -- order of execution
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/621542/compilers-and-argument-order-of-evaluation-in-c\">Compilers在C ++ 结果的评价参数顺序
COUT&LT;&LT;调用以功能它打印?
块引用>
本:
时int k = 3;
的printf(%D%D,K +,K + K);给出作为输出
4 4 4
,因为它们被压入堆栈:%D%d个4 - 对于k ++
4 - 对于ķ
4 - 对于++ķ右键?
本:
时int k = 3;
COUT&LT;&LT; ķ++&LT;&LT; K&LT;&LT; ++ K表;实际上是重复的函数调用,所以它等价于:
(((COUT&LT;&LT; k ++)LT;&LT; K)&LT;&LT; ++ K);
所以,我认为首先
K ++
然后K
,然后++ķ
必须始终按照这个顺序执行的,对不对?我相信,一个函数调用是一个序列点,但是输出上不同的实现而有所不同。为什么会这样?解决方案这是不确定的,因为在之间没有顺序点,在printf语句。如果没有一个序列点,编译器是免费的命令写入到存储器位置
K
,因为它的意志。现在你可能想知道'到底是seqeunce点,为什么它有关?基本上一个序列点在code点,其中有问题的内存位置,在这种情况下
K
已被修改,最多一次。有一个更全面的描述在这里:的http:// www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.16你可以从常见问题解答中,
,
看到的printf不引入序列点。在
的COUT
的情况下,因为有3个函数调用运营商GT,这是不同的;&GT;
。函数调用引入了一个序列点因此修改内存位置K
有一个定义的顺序。但是(这是一个点我错过了,但Cubbi指出),因为C / C ++没有定义的函数参数这些参数计算顺序,即使它们的功能,可以在编译器定义的顺序进行评估。所以在EX pression:
F(H(),G())
无论是H()或g()先求是不确定的:的 http://www2.research.att.com/~bs/bs_faq2.html#evaluation-order 。所以这就是为什么即使在COUT的情况下,你都可以从不同的编译器不同的结果,主要是因为在
COUT&LT的原因;&LT; ķ++&LT;&LT; K&LT;&LT; ++氏/ code>转化为
cout.operator&LT;&LT;(K +)运算符&LT;&LT;(K).operator(++ K)
这实际上是这样一个前pression:F(H(克(COUT,K +),K),+ K)
并在每个函数参数计算未指定的顺序。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.16As 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 tooperator >>
. A function call introduces a sequence point therefore the modifications to memory locationk
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 tocout.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屋!