C语言 printf中的“%.*s”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7899119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:00:41  来源:igfitidea点击:

What does "%.*s" mean in printf?

cprintfformat-specifiers

提问by Shaobo Wang

I got a code snippet in which there is a

我有一个代码片段,其中有一个

printf("%.*s\n")

what does the %.*smean?

是什么%.*s意思?

回答by AusCBloke

You can use an asterisk (*) to pass the width specifier/precision to printf(), rather than hard coding it into the format string, i.e.

您可以使用星号 ( *) 将宽度说明符/精度传递给printf(),而不是将其硬编码到格式字符串中,即

void f(const char *str, int str_len)
{
  printf("%.*s\n", str_len, str);
}

回答by Ondrej

More detailed here.

更详细的在这里

integer value or *that specifies minimum field width. The result is padded with space characters (by default), if required, on the left when right-justified, or on the right if left-justified. In the case when * is used, the width is specified by an additional argument of type int. If the value of the argument is negative, it results with the - flag specified and positive field width. (Note: This is the minimum width: The value is never truncated.)

.followed by integer number or *, or neither that specifies precision of the conversion. In the case when * is used, the precision is specified by an additional argument of type int. If the value of this argument is negative, it is ignored. If neither a number nor * is used, the precision is taken as zero. See the table below for exact effects of precision.

整数值或*指定最小字段宽度。结果用空格字符填充(默认情况下),如果需要,右对齐时在左侧,如果左对齐则在右侧。在使用 * 的情况下,宽度由 int 类型的附加参数指定。如果参数的值为负,则结果为 - 指定的标志和正的字段宽度。(注意:这是最小宽度:该值永远不会被截断。)

.后跟整数或 *,或两者都不指定转换的精度。在使用 * 的情况下,精度由 int 类型的附加参数指定。如果此参数的值为负,则将其忽略。如果既不使用数字也不使用 *,则精度为零。有关精度的确切影响,请参见下表。

So if we try both conversion specification

所以如果我们尝试两种转换规范

#include <stdio.h>

int main() {
    int precision = 8;
    int biggerPrecision = 16;
    const char *greetings = "Hello world";

    printf("|%.8s|\n", greetings);
    printf("|%.*s|\n", precision , greetings);
    printf("|%16s|\n", greetings);
    printf("|%*s|\n", biggerPrecision , greetings);

    return 0;
}

we get the output:

我们得到输出:

|Hello wo|
|Hello wo|
|     Hello world|
|     Hello world|

回答by rerun

I don't think the code above is correct but (according to this description of printf()) the .*means

我不认为上面的代码是正确的,但是(根据这个描述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.'

宽度未在格式字符串中指定,而是作为必须格式化的参数之前的附加整数值参数。

So it's a string with a passable width as an argument.

所以它是一个具有可传递宽度的字符串作为参数。

回答by Josh

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

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

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

sString of characters

.*精度未在格式字符串中指定,而是作为必须格式化的参数之前的附加整数值参数。

s字符串