像“1$"这样的位置参数如何?使用 printf()? [英] How do positional arguments like "1$" work with printf()?

查看:23
本文介绍了像“1$"这样的位置参数如何?使用 printf()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 man 我找到了

               printf("%*d", width, num);

               printf("%2$*1$d", width, num);

是等价的.

但 IMO 的第二种样式应该与以下相同:

But IMO the second style should be the same as:

               printf("%*d", num, width);

但是通过测试看来 man 是对的;为什么?

However via testing it seems man is right; why?

推荐答案

printf() 定义了这种行为:

The relevant part of the POSIX specification of printf() defines this behaviour:

转换可以应用于参数列表中格式之后的第n个参数,而不是下一个未使用的参数.在这种情况下,转换说明符字符 %(见下文)被序列%n$"替换,其中 n 是 [1,{NL_ARGMAX}] 范围内的十进制整数,给出参数在参数列表中的位置.此功能提供了格式字符串的定义,这些格式字符串以适合特定语言的顺序选择参数(请参阅示例部分).

Conversions can be applied to the nth argument after the format in the argument list, rather than to the next unused argument. 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. This feature provides for the definition of format strings that select arguments in an order appropriate to specific languages (see the EXAMPLES section).

格式可以包含编号的参数转换规范(即%n$"和*m$"),或未编号的参数转换规范(即%和 * ),但不是两者兼而有之.唯一的例外是 %% 可以与%n$"混合使用.形式.在格式字符串中混合编号和未编号的参数规范的结果是未定义的.当使用编号参数规范时,指定第 N 个参数要求所有前导参数,从第一个到第 (N-1) 个,都以格式指定字符串.

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. The only exception to this is that %% can be mixed with the "%n$" form. 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$"的格式字符串中转换规范的形式,参数列表中编号的参数可以根据需要从格式字符串中引用多次.

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$ 标识要打印其值的参数 - 在您的示例中为参数 2.

The %n$ identifies the argument whose value is to be printed - argument 2 in your example.

*n$ 标识其值将被视为格式宽度的参数 - 在您的示例中为参数 1.

The *n$ identifies the argument whose value is to be treated as the format width - argument 1 in your example.

因此,编写手册的人遵循了标准.

So, those writing the manual followed the standard.

你在评论中争论:

2$* 应该匹配第二个参数,而 1$d 应该匹配第一个参数,但事实证明 的情况并非如此printf("%2$*1$d", width, num);.

2$* should match the 2nd parameter while 1$d should match the first one, but it turns out that it's not true in the case of printf("%2$*1$d", width, num);.

如前所述,该标准清楚地将 n$ 部分附加为 %* 的后缀修饰符,而不是作为格式转换说明符(本例中为 d)和 *.您推定的设计可能会起作用,但不是您选择的设计.

As noted already, the standard clearly attaches the n$ parts as postfix modifiers of % and *, rather than as prefix modifiers of the format conversion specifier (d in this example) and *. Your putative design could, probably, be made to work, but was not the design chosen.

这篇关于像“1$"这样的位置参数如何?使用 printf()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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