What does this statement mean? printf ("[%. * s]", (int) length [i],

I read this page http://dev.mysql.com/doc/refman/5.0/en/mysql-fetch-row.html there is one line

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 it mean [%.*s]in this code?

+3
source share
3 answers

[%.*s]is a format string printfmeaning:

  • The first argument must be an integer (indicating the maximum length of the string to print).
  • the second argument must be the string itself.
  • [and ](and finite space) are transmitted as is.

Usually you see something like .7s, which means a 7-character string. Using *for length means taking it from the argument given.

, , , lengths[i], row[i] ( row[i] NULL, "NULL").

+7

%.*s - .

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

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

, , ( row[i] 'NULL', row[i] false) lengths[i]. , .

+1

[%.*s] - printf.

, printf() (row[i]), , (length[i]). .

see the documentation printf()for more information on format strings.

0
source

All Articles