#include <stdio.h>
int main() {
printf("%*d /n", 2, 20);
printf("%2$*1$d %1$d %2$d/n", 0, 1);
printf("%1$d %1$d %1$d",0); /*让单个参数显示3次! */
return 0;
}
运行结果:
20
1 0 1
0 0 0
man 3 printf 结果如下:
Format of the format string The format string is a character string, beginning and ending in its initial shift state, if any.
The format string is composed of zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetch-ing zero or more subsequent arguments. Each conversion specification is introduced by the character %, and ends with a conversion specifier. In between there may be (in this order) zero or more flags, an optional minimum field width, an optional precision and an optional length modifier.
下面这段没有完全理解,但已经知道用了。 注意这种用法只能运用于UNIX系统。
The arguments must correspond properly (after type promotion) with the conversion specifier. By default, the arguments are used in the order given, where each `*' and each conversion specifier asks for the next argument (and it is an error if insufficiently many arguments are given). One can also specify explicitly which argument is taken, at each place where an argument is required, by writing `%m$' instead of `%' and `*m$' instead of `*', where the decimal integer m denotes the position in the argument list of the desired argument, indexed starting from 1. Thus,
printf("%*d", width, num);
and
printf("%2$*1$d", width, num);
are equivalent. The second style allows repeated references to the same argument. The C99 standard does not include the style using `$', which comes from the Single Unix Specification. If the style using `$' is used, it must be used throughout for all conversions taking an argument and all width and precision arguments, but it may be mixed with `%%' formats which do not consume an argument.
There may be no gaps in the numbers of arguments specified using `$'; for example, if arguments 1 and 3 are specified, argument 2 must also be specified somewhere in the format string.
int main() {
printf("%*d /n", 2, 20);
printf("%2$*1$d %1$d %2$d/n", 0, 1);
printf("%1$d %1$d %1$d",0); /*让单个参数显示3次! */
return 0;
}
运行结果:
20
1 0 1
0 0 0
man 3 printf 结果如下:
Format of the format string The format string is a character string, beginning and ending in its initial shift state, if any.
The format string is composed of zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetch-ing zero or more subsequent arguments. Each conversion specification is introduced by the character %, and ends with a conversion specifier. In between there may be (in this order) zero or more flags, an optional minimum field width, an optional precision and an optional length modifier.
下面这段没有完全理解,但已经知道用了。 注意这种用法只能运用于UNIX系统。
The arguments must correspond properly (after type promotion) with the conversion specifier. By default, the arguments are used in the order given, where each `*' and each conversion specifier asks for the next argument (and it is an error if insufficiently many arguments are given). One can also specify explicitly which argument is taken, at each place where an argument is required, by writing `%m$' instead of `%' and `*m$' instead of `*', where the decimal integer m denotes the position in the argument list of the desired argument, indexed starting from 1. Thus,
printf("%*d", width, num);
and
printf("%2$*1$d", width, num);
are equivalent. The second style allows repeated references to the same argument. The C99 standard does not include the style using `$', which comes from the Single Unix Specification. If the style using `$' is used, it must be used throughout for all conversions taking an argument and all width and precision arguments, but it may be mixed with `%%' formats which do not consume an argument.
There may be no gaps in the numbers of arguments specified using `$'; for example, if arguments 1 and 3 are specified, argument 2 must also be specified somewhere in the format string.
本文详细解析了C语言中printf函数的格式字符串用法,包括普通字符复制、转换规范、标志符、最小字段宽度等,并介绍了如何使用美元符号($)+数字索引来指定参数位置,允许重复引用相同的参数。
1338

被折叠的 条评论
为什么被折叠?



