简体   繁体   English

这句话是什么意思? printf(“[%。* s]”,(int)length [i],

[英]What does this statement mean ? printf(“[%.*s] ”, (int) lengths[i],

I was reading this page http://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-row.html there is one line 我正在阅读这个页面http://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-row.html有一行

printf("[%.*s] ", (int) lengths[i],
              row[i] ? row[i] : "NULL");

from code 来自代码

    MYSQL_ROW row;
unsigned int num_fields;
unsigned int i;

num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result)))
{
   unsigned long *lengths;
   lengths = mysql_fetch_lengths(result);
   for(i = 0; i < num_fields; i++)
   {
       printf("[%.*s] ", (int) lengths[i],
              row[i] ? row[i] : "NULL");
   }
   printf("\n");

} }

what does [%.*s] mean in that code ? [%.*s]在该代码中的含义是什么?

[%.*s] is a printf format string meaning: [%.*s]是一个printf格式字符串,意思是:

  • the first argument should be an integer (specifying maximum length of a string to print). 第一个参数应该是一个整数(指定要打印的字符串的最大长度)。
  • the second argument should be the string itself. 第二个参数应该是字符串本身。
  • the [ and ] (and trailing space) are transferred as-is. [] (和尾随空格)按原样传输。

Normally, you would see something like .7s which means a 7-character string. 通常情况下,您会看到类似.7s ,这意味着7个字符的字符串。 The use of * for the length means to take it from the argument given. 使用*表示长度意味着从给出的参数中获取它。

So what that entire line does is to print a string , the length of which is found in lengths[i] , and the value of which is row[i] (unless row[i] is NULL, in which case it uses the literal string "NULL" ). 那么整行的作用是打印一个字符串,其长度在lengths[i] ,其值为row[i] (除非row[i]为NULL,在这种情况下它使用文字字符串"NULL" )。

%.*s is an output format string. %.*s是输出格式字符串。

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

printf("[%.*s] ", (int) lengths[i], row[i] ? row[i] : "NULL"); 

Specifically in this case it means to print the the second argument (contents of row[i] or 'NULL' if contents of row[i] evaluate to false) with a maximum of lengths[i] characters. 具体地说,在这种情况下,它意味着打印第二个参数( row[i]或'NULL'的内容,如果row[i]评估为false),最大lengths[i]字符。 The square brackets are not part of the formatting, they get printed themselves 方括号不是格式的一部分,它们自己打印

the [%.*s] part is a format string for printf. [%.*s]部分是printf的格式字符串。

it specifies that printf() should output a string ( row[i] ) but should limit the output to the length specified by a parameter ( length[i] ). 它指定printf()应输出一个字符串( row[i] ),但应将输出限制为参数( length[i] )指定的length[i] the output string is enclosed inside square brackets. 输出字符串括在方括号内。

see the printf() documentation for more information on format strings. 有关格式字符串的更多信息,请参阅printf()文档。

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

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