它是非法使用h或hh长度修改时,相应的参数的printf不是一个短/字符? [英] Is it illegal to use the h or hh length modifiers when the corresponding argument to printf was not a short / char?

查看:139
本文介绍了它是非法使用h或hh长度修改时,相应的参数的printf不是一个短/字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的printf 系列函数提供一系列长度修改的,其中两个是 HH (表示一个符号字符晋升为unsigned char型参数 INT )和 ^ h (表示一个符号短无符号短参数提升为 INT )。从历史上看,这些长度修改只被引入到与 scanf函数的长度修改创建对称,很少用于的printf

The printf family of functions provide a series of length modifiers, two of them being hh (denoting a signed char or unsigned char argument promoted to int) and h (denoting a signed short or unsigned short argument promoted to int). Historically, these length modifiers have only been introduced to create symmetry with the length modifiers of scanf and are rarely used for printf.

下面是ISO的9899摘录时间:2011年§7.21.6.1fprintf函数¶7:

Here is an excerpt of ISO 9899:2011 §7.21.6.1 "The fprintf function" ¶7:

7的长度改性剂和它们的含义是:

7 The length modifiers and their meanings are:


      
  • HH 指定后续的 D I 0 U X X 转换说明适用于符号字符 unsigned char型参数(参数将被按照整数促销推广,但其价值将被转换为符号字符 unsigned char型打印前);或者说一个如下的 N 转换说明适用于指向一个符号字符
      参数。

  • hh Specifies that a following d, i, o, u, x, or X conversion specifier applies to a signed char or unsigned char argument (the argument will have been promoted according to the integer promotions, but its value shall be converted to signed char or unsigned char before printing); or that a following n conversion specifier applies to a pointer to a signed char argument.

^ h 指定后续的 D I 0 U X X 转换说明适用于短整型无符号的短整型参数(该参数将被按照整数促销推广,但其价值将被转换为短整型无符号的短整型打印前);或者说一个如下的 N 转换说明适用于指针短 INT 参数。

h Specifies that a following d, i, o, u, x, or X conversion specifier applies to a short int or unsigned short intargument (the argument will have been promoted according to the integer promotions, but its value shall be converted to short int or unsigned short int before printing); or that a following n conversion specifier applies to a pointer to a short int argument.

...

忽略的情况下, N 转换说明,做这些几乎相同的段落说一下的 h时的行为 HH

Ignoring the case of the n conversion specifier, what do these almost identical paragraphs say about the behaviour of h and hh?


  • 这个答案,它声称,传递一个论点是符号字符,符号短 unsigned char型无符号短 RESP。对于具有 ^ h HH 长度修改RESP转换规范。是的未定义行为,的作为参数不是从类型转换字符,等RESP。之前。

  • 我要求该函数在一个定义良好的方式类型的每个值运行 INT 的printf 表现为如果参数转化为字符等RESP。之前转换。

  • 一个可以还声称,与那是以前默认参数推广相应类型的不是一个参数调用该函数是不确定的行为,但这似乎深奥。<​​/ li>
  • In this answer, it is claimed that passing an argument that is outside the range of a signed char, signed short, unsigned char, or unsigned short resp. for a conversion specification with an h or hh length modifier resp. is undefined behaviour, as the argument wasn't converted from type char, short, etc. resp. before.
  • I claim that the function operates in a well-defined manner for every value of type int and that printf behaves as if the parameter was converted to char, short, etc. resp. before conversion.
  • One could also claim that invoking the function with an argument that was not of the corresponding type before default argument promotion is undefined behaviour, but this seems abstruse.

哪个§7.21.6.1¶7这三间pretations的(如果有的话)是正确的?

Which of these three interpretations of §7.21.6.1¶7 (if at all) is correct?

推荐答案

本标准规定了:

如果任何参数都不是正确的类型对应的转换规范,行为是不确定的。

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

[C2011 7.21.6.1/9]

[C2011 7.21.6.1/9]

什么是正确的类型的意思,是间pretation可以想象开放,但最合理的间pretation我是类型的转换规范适用于作为较早在指定相同部分,并作为引,部分中的问题。我坐的括号的有关参数推广注释中承认普通的参数传递规则,避免这些功能是特殊情况下的任何暗示。我不走插句批示涉及确定参数的正确的类型。

What is meant by "the correct type", is conceivably open to interpretation, but the most plausible interpretation to me is the type that the conversion specification "applies to" as specified earlier in the same section, and as quoted, in part, in the question. I take the parenthetical comments about argument promotion to be acknowledging the ordinary argument-passing rules, and avoiding any implication of these functions being special cases. I do not take the parenthetic comments as relevant to determining the "correct type" of the argument.

如果您通过更广泛类型的参数比对转换规范正确的实际情况是不同的问题。我倾向于认为,空调系统是不可能被任何人实现,使得它的确与众不同无论是的printf()的说法实际上是一个,或者它是否是一个 INT ,其值是在字符的范围内。我断言,但是,它是有效的行为为编译器检查参数类型对应的格式,并拒绝该程序,如果有一个不匹配(因为在这种情况下所要求的行为是明确未定义)。

What actually happens if you pass an argument of wider type than is correct for the conversion specification is a different question. I am inclined to believe that the C system is unlikely to be implemented by anybody such that it makes a difference whether a printf() argument is actually a char, or whether it is an int whose value is in the range of char. I assert, however, that it is valid behavior for the compiler to check argument type correspondence with the format, and to reject the program if there is a mismatch (because the required behavior in such a case is explicitly undefined).

在另一方面,我当然可以想像的printf()实际表现不好的实现(打印垃圾,损坏内存,吃你的午餐)如果参数的值由相应的转换指定暗示的范围之外。这也是允许的帐户被未定义行为。

On the other hand, I could certainly imagine printf() implementations that actually misbehave (print garbage, corrupt memory, eat your lunch) if the value of an argument is outside the range implied by the corresponding conversion specifier. This also is permissible on account of the behavior being undefined.

这篇关于它是非法使用h或hh长度修改时,相应的参数的printf不是一个短/字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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