如何使用格式说明符设置字符串的字段宽度?
How to use format specifiers to set field width of a string?
printf("%5s\n", "#");
给出:
#
他们是否可以使用整数格式说明符设置此字符串的字段宽度?
像这样,
printf("%%ds\n", 5, "#");
Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer m) to specify that the field width is given in the next argument, or in the m-th argument, respectively, which must be of type int.
因此在您的示例中它将是:
printf("%*s\n", 5, "#");
printf("%5s\n", "#");
给出:
#
他们是否可以使用整数格式说明符设置此字符串的字段宽度?
像这样,
printf("%%ds\n", 5, "#");
Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer m) to specify that the field width is given in the next argument, or in the m-th argument, respectively, which must be of type int.
因此在您的示例中它将是:
printf("%*s\n", 5, "#");