如何定位参数,像" 1 $"与工作的printf()? [英] How do positional arguments like "1$" work with printf()?

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

问题描述

通过我觉得

               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);

不过通过测试似乎是正确的;为什么?

推荐答案

的<一的POSIX规范的相关部分href=\"http://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html\"><$c$c>printf()定义这种行为:

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

转换可个参数后应用于名词的的格式的参数列表,而不是下一个未使用的参数。在这种情况下,转换指定字符%(见下文)由序列%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-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,宽,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 $ 部分为的后缀修饰和 * ,而不是作为格式转换说明的preFIX改性剂( 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.

这篇关于如何定位参数,像&QUOT; 1 $&QUOT;与工作的printf()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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