简体   繁体   English

如何在C中解释此printf

[英]How do I interpret this printf in C

I am going through some code where I got stuck reading a printf statement: 我正在阅读一些代码,在那里我不得不阅读printf语句:

printf("%*s%s\n", stat[type].dent, "", buf);

states[type].indent is a number which is equal to 4 and buf is buffer of 215 bytes with 0 strored into it as string but not sure "" and %*s is meant for. states[type].indent是一个等于4的数字,而buf是215字节的缓冲区,其中0作为字符串存储在字符串中,但不确定""%*s的含义。

Can anybody help me reading this printf statement? 有人可以帮助我阅读此printf声明吗?

The * in the format code "%*s" tells printf that the next argument is a field width followed by the normal argument (a string in this case). *在格式代码"%*s"告诉printf下一个参数是一个字段的宽度,然后在正常参数(在这种情况下,字符串)。

It's the same as eg "%4s" in your case, but the width can be set in runtime. 与您的情况下例如"%4s"相同,但是宽度可以在运行时设置。

So here stat[type].dent is the field width, and the empty string is the string. 因此,这里stat[type].dent是字段宽度,空字符串是字符串。 So this prints the empty string with 4 characters width (so you get four spaces) followed by whatever is in buf . 因此,这将打印出4个字符宽度的空字符串(因此您将获得四个空格),然后打印buf

I recommend a good reference on printf where it's all documented. 我建议所有记录的printf上提供很好的参考

The * means that the width of the argument to be printed is dependent on an (integer) argument that precedes it. *表示要打印的参数的宽度取决于其前面的(整数)参数。

From cplusplus : 来自cplusplus

printf ("Width trick: %*d \n", 5, 10); // 5 is the width, 10 is the integer being printed

The %*s is a string conversion. %*s是字符串转换。 The * part means that a width for the field will be supplied as a parameter (that's how stat[type].dent is used). *部分表示字段的宽度将作为参数提供(这就是使用stat[type].dent方式)。 So it's printing an empty string in some specified width (call it N). 因此,它以指定的宽度打印一个空字符串(称为N)。 In other words, its leaves N blank spaces before printing out buf . 换句话说,在打印出buf之前,它会留下N个空格。

*将被stat[type].dent的值代替,该值是一个数字,称为最小字段宽度。

It is a precision, which specifies maximum number of bytes for string conversions. 这是一种精度,它指定用于字符串转换的最大字节数。

Asterisk (*), uses an integer argument, which specifies the value (for precision) to be used. 星号(*)使用整数参数,该参数指定要使用的值(精度)。

To print a string for a variable length,specify printf("%*s", l, string) . 要打印可变长度的字符串,请指定printf("%*s", l, string) The l is substituted for the asterisk. l代替星号。

"%*s%s\\n" is a format that contains 3 directives . "%*s%s\\n"是一种包含3个指令的格式。

"%*s"

This directive is the string conversion specification. 此伪指令是字符串转换规范。 The * causes the field width, an int , to be determined from the the next parameter after the format. *使字段宽度int从格式之后的下一个参数确定。 As OP mentions, this is the value of 4 from stat[type].dent . 如OP所述,这是stat[type].dent的4值。 The s conversion specification then gets the next parameter ( "" ) and treats it a pointer to char * , which it is. 然后, s转换规范获取下一个参数( "" )并将其视为指向char *的指针。 Thus printf() prints out "" , pre-padding first with spaces, up to 4, as needed. 因此, printf()打印出"" ,并根据需要先填充最多4个空格。 4 padding spaces are needed here as the string length of "" is 0. Net result: 4 spaces printed. 这里需要4个填充空格,因为字符串""长度为0。最终结果:打印了4个空格。

"%s"

This directive, also a string conversion specification, takes the next parameter, buf , assumes it is a char * , which it is, and prints its contents out. 该指令也是一个字符串转换规范,它使用下一个参数buf ,假设它是char * ,并输出其内容。 Unclear if OP means it is all 0, if so, then nothing is printed. 不清楚OP是否表示全为 0,如果是,则不打印任何内容。

"\\n"

This final directive is simply a text, so "\\n" is printed. 该最终指令仅是文本,因此将打印"\\n"

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

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