什么是*%s格式说明符的意思吗? [英] What does the %*s format specifier mean?

查看:496
本文介绍了什么是*%s格式说明符的意思吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一些code,我有维护,我见过的格式说明%* S 。谁能告诉我这是什么,以及为什么使用它?

其使用的一个例子是这样的:

  fprintf中(不过outFile,\\ N%* S,缩进,);


解决方案

它用于指定,在动态方式,什么字段的宽度是:在


  

      
  • 未在格式字符串指定的宽度,但作为附加
      整数值参数preceding的
      有说法进行格式化。

  •   

所以缩进指定多少空间分配给它后面的参数列表中的字符串。

因此​​,

 的printf(%* S,5,);

是相同的

 的printf(%5S,);

这是把一些空间在文件中一个不错的方式,避免了循环。

In some code that I have to maintain, I have seen a format specifier %*s . Can anybody tell me what this is and why it is used?

An example of its usage is like:

fprintf(outFile, "\n%*s", indent, "");

解决方案

It's used to specify, in a dynamic way, what the width of the field is:

  • The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

so "indent" specifies how much space to allocate for the string that follows it in the parameter list.

So,

printf("%*s", 5, "");

is the same as

printf("%5s", "");

It's a nice way to put some spaces in your file, avoiding a loop.

这篇关于什么是*%s格式说明符的意思吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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