这是未定义的行为或实现定义吗? [英] Is this undefined behavior or implementation defined?

查看:79
本文介绍了这是未定义的行为或实现定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是以下未定义或实现定义的:

Is the following undefined or implementation-defined:

 int x = 0;
 printf("%d%d", ++x, x);

计算参数的顺序未指定,因此:

The order of evaluating arguments is unspecified, so:


  • 如果首先评估 ++ x ,则会输出 11 。 / li>
  • 如果首先评估 x ,则会打印 10
  • if ++x is evaluated first, this prints 11.
  • if x is evaluated first, it prints 10.

推荐答案

printf("%d%d", ++x, x);

这显然是C ++中未定义的行为。

This is clearly undefined behavior in C++.

(C ++ 11,1.9p15)如果对标量
对象的副作用相对于对同一标量对象或值计算的另一个副作用是无效的$ b使用相同标量对象的值,行为是未定义的。

(C++11, 1.9p15) "If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined."

p>

Same for C (emphasis mine):


(C99,6.5.p2)在上一个和下一个序列点之间,一个对象的存储值最多修改一次此外,先前值应该只读,以确定要存储的值 .73

请注意,C11现在与C ++ 11中的语句类似:

Note that C11 has now a similar wording as in C++11:


(C11,6.5p2)对于标量对象的副作用相对于对同一标量对象的不同副作用或使用同一标量对象的值的计算是不确定的,该行为是未定义的。

(C11, 6.5p2) "If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined."

这篇关于这是未定义的行为或实现定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆