确实的printf(QUOT;%X",1)调用未定义行为? [英] Does printf("%x",1) invoke undefined behavior?

查看:151
本文介绍了确实的printf(QUOT;%X",1)调用未定义行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据C标准(6.5.2.2第6款)

According to the C standard (6.5.2.2 paragraph 6)

如果该表示称为功能恩pression有一个类型不包括一
  原型,整数促销活动是在每个参数执行,论点
  有float类型都提升到两倍。这些被称为默认参数
  促销。如果参数的数量不等于的参数,数
  行为是理解过程网络定义。如果函数是一个类型,其中包括一个原型定义去Fi和
  无论是样机以省略号(...)或之后参数类型结束
  促销不是同类型的参数兼容,行为定义理解过程网络。
  如果函数是与类型定义德音响不包括一个原型,和类型的
  升级后的参数是不能与那些参数后兼容
  推广,行为是理解过程网络定义,除下列情况:

If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions. If the number of arguments does not equal the number of parameters, the behavior is undefined. If the function is defined with a type that includes a prototype, and either the prototype ends with an ellipsis (, ...) or the types of the arguments after promotion are not compatible with the types of the parameters, the behavior is undefined. If the function is defined with a type that does not include a prototype, and the types of the arguments after promotion are not compatible with those of the parameters after promotion, the behavior is undefined, except for the following cases:


      
  • 一个提升的类型是有符号整数类型,其他类型的推广是
      对应的无符号整数类型,并且该值是在两种类型的重新presentable;

  •   
  • 这两种类型都指向合格网络版或字符类型的unquali网络版版本或
      无效的。

  •   

因此​​,在一般情况下,没有什么不妥传递一个 INT 给需要 unsigned int类型(反之亦然),只要值传递这两种类型的配合。但是,printf的规范上写着(7.19.6.1第9段):

Thus, in general, there is nothing wrong with passing an int to a variadic function that expects an unsigned int (or vice versa) as long as the value passed fits in both types. However, the specification for printf reads (7.19.6.1 paragraph 9):

如果一个转换说明是无效的,行为是理解过程网络定义。如果任何参数
  不正确的类型对应的转换特定网络阳离子,行为
  理解过程网络斯内德。

If a conversion specification is invalid, the behavior is undefined. If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

没有例外是符号/无符号不匹配的。

No exception is made for signed/unsigned mismatch.

这是否意味着的printf(%X,1)调用未定义的行为?

Does this mean that printf("%x", 1) invokes undefined behavior?

推荐答案

我相信这在技术上是不确定的,因为正确的类型为%X 指定为 unsigned int类型 - 正如你指出的,对于在这里签署/无符号不匹配也不例外。

I believe it is technically undefined, because the "correct type" for %x is specified as unsigned int - and as you point out, there is no exception for signed/unsigned mismatch here.

的printf 的规则是用于更具体的情况下,从而覆盖的一般情况的规则(用于特定覆盖一般的另一个例子,它在一般允许的通过 NULL 来一个函数期待为const char * 参数,但它是不确定的行为传递 NULL 的strlen())。

The rules for printf are for a more specific case and thus override the rules for the general case (for another example of the specific overriding the general, it's allowable in general to pass NULL to a function expecting a const char * argument, but it's undefined behaviour to pass NULL to strlen()).

我说:在技术上,因为我相信一个实现将需要故意不当造成这种情况的一个问题,因为在标准的其他限制。

I say "technically", because I believe an implementation would need to be intentionally perverse to cause a problem for this case, given the other restrictions in the standard.

这篇关于确实的printf(QUOT;%X",1)调用未定义行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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