printf("[%.*s] ",...这是什么意思?
mysql c api 文档里面有这么一句
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");
}
请问下,printf("[%.*s] " 是什么意思,我输出的时候没有把(int) lengths[i]显示出来。谢谢!
[解决办法]
A field width, or precision, or both, may be indicated by an asterisk ( '*' ). In
this case an argument of type int supplies the field width or precision. Applica-
tions shall ensure that arguments specifying field width, or precision, or both
appear in that order before the argument, if any, to be converted. A negative field
width is taken as a '-' flag followed by a positive field width. A negative preci-
sion is taken as if the precision were omitted. In format strings containing the
"%n$" form of a conversion specification, a field width or precision may be indi-
cated by the sequence "*m$", where m is a decimal integer in the range
[1,{NL_ARGMAX}] giving the position in the argument list (after the format argument)
of an integer argument containing the field width or precision, for example:
printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec);
[解决办法]
查MSDN是Windows程序员必须掌握的技能之一。
mk:@MSITStore:D:\MSDN98\98VS\2052\vccore.chm::/html/_crt_printf_width_specification.htm
printf Width Specification
The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the flag (for left alignment) is specified — until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).
The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification).
If the width specification is an asterisk (*), an int argument from the argument list supplies the value. The width argument must precede the value being formatted in the argument list. A nonexistent or small field width does not cause the truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result.