cplusplus.com以上的人给出了printf的参考资料:
printf("%[flags][width][.precision][length]specifier", "Message");
但是如果我想要预先添加一些空格,我将不得不使用%[number of spaces]s, "" ...etc
是否有更清晰的方法来预置空格,而没有笨拙地预先添加空字符串?
答案 0 :(得分:3)
使用*
指定总宽度。没有前置的空字符串。
const char *message = "Message";
int number_of_spaces = 3;
int width = number_of_spaces + strlen(message);
printf("<%*s>\n", width, message);
输出
< Message>
答案 1 :(得分:1)
您可以使用printf("%15s", "");
仅打印15个空格。
您可以使用printf("%15s%d\n", "", number);
打印一个前面有15个空格的数字。