简体   繁体   English

为格式说明符传递的printf参数太少 - 是不确定的行为?

[英]Passing too few arguments to printf for the format specifier — is it undefined behavior?

As I know, having too few format specifiers for the arguments is considered as undefined behavior (and having too few arguments for the format specifiers is also undefined behaviour). 据我所知,参数的格式说明符太少被认为是未定义的行为(并且格式说明符的参数太少也是未定义的行为)。 Does this apply to: 这适用于:

printf("%1$d %1$d", 5);

PS Use of "%1$d" as format specifier is an extension to the printf family of functions added by POSIX . PS使用"%1$d"作为格式说明符是POSIX添加的printf系列的扩展

The n$ notation is not part of standard C, but is part of POSIX. n$表示法不是标准C的一部分,但是是POSIX的一部分。 The POSIX specification for printf() supports the n$ notation to refer to arguments. printf()的POSIX规范支持n$表示法来引用参数。

Conversions can be applied to the nth argument after the format in the argument list, rather than to the next unused argument. 转换可以应用于参数列表中的格式之后的第n个参数,而不是下一个未使用的参数。 In this case, the conversion specifier character % (see below) is replaced by the sequence "%n$" , where n is a decimal integer in the range [1,{NL_ARGMAX}], giving the position of the argument in the argument list. 在这种情况下,转换说明符字符% (见下文)被序列"%n$"替换,其中n是[1,{NL_ARGMAX}]范围内的十进制整数,给出参数在参数中的位置名单。 This feature provides for the definition of format strings that select arguments in an order appropriate to specific languages (see the EXAMPLES section). 此功能提供了格式字符串的定义,该字符串以适合特定语言的顺序选择参数(请参阅示例部分)。

The format can contain either numbered argument conversion specifications (that is, "%n$" and "*m$" ), or unnumbered argument conversion specifications (that is, % and * ), but not both. 格式可以包含编号参数转换规范(即"%n$""*m$" ),或无编号参数转换规范(即%* ),但不能同时包含两者。 The only exception to this is that %% can be mixed with the "%n$" form. 唯一的例外是%%可以与"%n$"表单混合使用。 The results of mixing numbered and unnumbered argument specifications in a format string are undefined. 在格式字符串中混合编号和未编号参数规范的结果是未定义的。 When numbered argument specifications are used, specifying the Nth argument requires that all the leading arguments, from the first to the (N-1)th, are specified in the format string. 当使用带编号的参数规范时,指定第N个参数要求在格式字符串中指定从第一个到第(N-1)个的所有前导参数。

In format strings containing the "%n$" form of conversion specification, numbered arguments in the argument list can be referenced from the format string as many times as required. 在包含转换规范的"%n$"形式的格式字符串中,参数列表中的编号参数可以根据需要多次从格式字符串中引用。

It requires that you provide an argument for each n$ , and that the format string refers to every argument 1..n. 它要求您为每个n$提供一个参数,并且格式字符串引用每个参数1..n。 It doesn't say you have to use a different n$ each time. 它并没有说你每次都必须使用不同的n$

The code shown is fine on POSIX systems. 显示的代码在POSIX系统上很好。 Since it uses a POSIX-only feature, it won't be portable to non-POSIX systems that don't have the necessary support as an extension. 由于它仅使用POSIX功能,因此无法将其移植到没有必要支持作为扩展的非POSIX系统。

This is valid in POSIX , which says: 这在POSIX中有效,它说:

In format strings containing the "%n$" form of conversion specification, numbered arguments in the argument list can be referenced from the format string as many times as required. 在包含转换规范的“%n $”形式的格式字符串中,参数列表中的编号参数可以根据需要多次从格式字符串中引用。

(emphasis mine) (强调我的)

There are constraints on the %n$ format specifier but are not relevant to the question. %n$格式说明符存在约束,但与问题无关。 See the linked POSIX manual for them. 请参阅链接的POSIX手册。

But a strict reading of C standard appears to say this is undefined . 严格阅读 C标准似乎表明这是不确定的

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

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