简体   繁体   English

%*s 格式说明符是什么意思?

[英]What does the %*s format specifier mean?

In some code that I have to maintain, I have seen a format specifier %*s .在我必须维护的一些代码中,我看到了格式说明符%*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.宽度未在格式字符串中指定,而是作为必须格式化的参数之前的附加 integer 值参数指定。

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.这是在文件中放置一些空格以避免循环的好方法。

Don't use "%*s" on a buffer which is not NULL terminated (packed) thinking that it will print only "length" field.不要在不是 NULL 终止(打包)的缓冲区上使用“%*s”,认为它只会打印“长度”字段。

The format specifier %4s outputs a String in a field width of 4—that is, printf displays the value with at least 4 character positions.格式说明符 %4s 输出字段宽度为 4 的字符串,即 printf 显示具有至少 4 个字符位置的值。

If the value to be output is less than 4 character positions wide, the value is right justified in the field by default.如果 output 的值less 4 个字符位置宽,默认情况下该值在字段中right justified

If the value is greater than 4 character positions wide, the field width expands to accommodate the appropriate number of characters.如果该值greater 4 个字符位置宽,则字段宽度expands以容纳适当的字符数。

To left justify the value, use a negative integer to specify the field width.要左对齐该值,请使用负值 integer 指定字段宽度。

References: Java™ How To Program (Early Objects), Tenth Edition参考资料: Java™ 如何编程(早期对象),第十版

When used in printf and fprintf:在 printf 和 fprintf 中使用时:

printf("%*s", 4, myValue); is equivalent to printf("%4s", myValue);

It displays the variable with minimum width, rest right-justified spaces.它显示具有最小宽度的变量,rest 个右对齐空格。 To left-justify the value, use a negative integer.要左对齐该值,请使用负数 integer。

When used in scanf and sscanf:在 scanf 和 sscanf 中使用时:

/* sscanf example */
#include <stdio.h>

int main ()
{
  char sentence []="Rudolph is 12 years old";
  char str [20];
  int i;

  sscanf (sentence,"%s %*s %d",str,&i);
  printf ("%s -> %d\n",str,i);
  
  return 0;
}

Output: Output:

Rudolph -> 12

It is used to ignore a string.它用于忽略字符串。

* Causes fprintf to pad the output until it is n characters wide, where n is an integer value stored in the a function argument just preceding that represented by the modified type. * 使 fprintf 填充 output 直到它有 n 个字符宽,其中 n 是存储在 function 参数中的 integer 值,就在修改后的类型所代表的参数之前。

printf("%*d", 5, 10) //will result in "10" being printed with a width of 5.

http://www.cplusplus.com/reference/clibrary/cstdio/printf/ http://www.cplusplus.com/reference/clibrary/cstdio/printf/

The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.宽度未在格式字符串中指定,而是作为必须格式化的参数之前的附加 integer 值参数指定。

eg: printf("%*s", 4, myValue);例如: printf("%*s", 4, myValue); is equivelant to printf("%4s", myValue);等同于printf("%4s", myValue); . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM