简体   繁体   English

你能解释输出吗?

[英]Can you explain the Output?

What should be the output of the following code and why? 以下代码的输出应该是什么?为什么? I am little bit confused. 我有点困惑。

int a =10;
printf("%d %d %d",a,a=a+10,a);

The output is indeterminate, because a=a+10 is a side-effect, and the compiler is free to evaluate it before or after any of the other parameters. 输出是不确定的,因为a=a+10是副作用,编译器可以在任何其他参数之前或之后自由地评估它。

EDIT: As David points out, the behaviour is actually undefined , which means all bets are off and you should never write such code. 编辑:正如大卫指出的那样,行为实际上是未定义的 ,这意味着所有的赌注都已关闭,你永远不应该编写这样的代码。 In practice, the compiler will almost always do something plausible and unpredictable, maybe even differing between debug and optimised builds. 在实践中,编译器几乎总是会做一些看似合理且不可预测的事情,甚至可能在调试和优化构建之间有所不同。 I don't think a sperm whale is a likely outcome. 我认为抹香鲸不是一种可能的结果。 Petunias? 牵牛花? Perhaps. 也许。

The order of evaluation for a, b, and c in a function call f(a,b,c) is unspecified. 函数调用f(a,b,c) a,b和c的求值顺序未指定。

Read about sequence points to get a better idea: (The undefined behavior in this particular case is not due to sequence points. Thanks to @stusmith for pointing that out) 阅读序列点以获得更好的想法:(在这种特殊情况下未定义的行为不是由于序列点。感谢@stusmith指出这一点)

A sequence point in imperative programming defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. 命令式编程中的序列点定义了计算机程序执行中的任何点,在该点处保证先前评估的所有副作用都已执行,并且尚未执行后续评估的副作用。 They are often mentioned in reference to C and C++, because the result of some expressions can depend on the order of evaluation of their subexpressions. 它们经常在引用C和C ++时被提及,因为某些表达式的结果可能取决于它们的子表达式的评估顺序。 Adding one or more sequence points is one method of ensuring a consistent result, because this restricts the possible orders of evaluation. 添加一个或多个序列点是确保结果一致的一种方法,因为这会限制可能的评估顺序。

Sequence points also come into play when the same variable is modified more than once. 当同一个变量被多次修改时,序列点也会发挥作用。 An often-cited example is the expression i=i++ , which both assigns i to itself and increments i ; 一个经常被引用的例子是表达式i=i++ ,它既赋予i自身又增加i ; what is the final value of i ? 什么是最终价值i Language definitions might specify one of the possible behaviors or simply say the behavior is undefined. 语言定义可能指定一种可能的行为,或者只是说行为未定义。 In C and C++, evaluating such an expression yields undefined behavior . 在C和C ++中,评估这样的表达式会产生不确定的行为

Thanks for the answers.... :) The behavior is really undefined and compiler dependent. 谢谢你的答案.... :)行为是真的未定义和编译器相关。 Here are some outputs 这是一些输出

Compiled with Turbo c : 20 20 10 用Turbo编译c:20 20 10

Compiled with Visual Studio c++: 20 20 20 用Visual Studio c ++编译:20 20 20

Compiled with CC: 20 20 20 用CC编写:20 20 20

Compiled with gcc: 20 20 20 用gcc编译:20 20 20

Compiled with dev c++: 20 20 10 用dev c ++编译:20 20 10

Not defined. 没有定义的。
The evaluation order of a function parameters is not defined by the standard. 功能参数的评估顺序不是由标准定义的。
So the output of this could be anything. 所以这个输出可能是任何东西。

在Bloodshed Dev C ++中使用Mingw Compiler:20 20 10

Not to amend previous correct answers, but a little additional information: according to the Standard, even this would be undefined: 不是为了修改以前的正确答案,而是提供一些额外的信息:根据标准,即使这样也是未定义的:

int a =10;
printf("%d %d %d", a = 20, a = 20, a = 20);

It is highly compiler dependent . 高度依赖于编译器

Because evaluation order of arguments is not specified by standard. 因为参数的评估顺序不是由标准指定的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM