Reputation: 639
The people over at cplusplus.com give the reference for printf as so:
printf("%[flags][width][.precision][length]specifier", "Message");
but if I wanted to to prepend some spaces, I would have to use %[number of spaces]s, "" ...etc
is there a clearer way to prepend spaces, without clunkily prepending empty strings?
Upvotes: 1
Views: 74
Reputation: 153367
Use *
to specify the total width. No prepended empty strings.
const char *message = "Message";
int number_of_spaces = 3;
int width = number_of_spaces + strlen(message);
printf("<%*s>\n", width, message);
Output
< Message>
Upvotes: 3
Reputation: 206577
You can use printf("%15s", "");
to print just 15 spaces.
You can use printf("%15s%d\n", "", number);
to print a number with 15 spaces prepended to the output.
Upvotes: 1