How can I achieve fixed length padding with trailing spaces by using sprintf?
For example, how can I get sprintf(buf, "The number is:%i", n) to output 50 characters with trailing spaces?
My man 3 printf says:
An optional decimal digit string specifying a minimum field width. If the converted value has fewer characters than the field width, it will be padded with spaces on the left (or right, if the left-adjustment flag has been given) to fill out the field width.
and
`-' A negative field width flag; the converted value is to be left adjusted on the field boundary. Except for n conver- sions, the converted value is padded on the right with blanks, rather than on the left with blanks or zeros. A - overrides a 0 if both are given.
and
A field width or precision, or both, may be indicated by an asterisk '*' or an asterisk followed by one or more decimal digits and a '$' instead of a digit string. In this case, an int argument supplies the field width or precision.
So I tried:
#include <stdio.h>
#include <string.h>
int main() {
int num = 123;
char* text = "The number is: ";
printf("%s%-*iX\n", text, 50 - (int) strlen(text), num);
}
That gets:
The number is: 123 X
(Where the X is just a marker to show the spaces for padding.)
man 3 printf(on Unix)? Put "printf" in the search engine of your choice?