printf参数的复用

#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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值