Set variable text column width in printf

You can do this as follows:

printf("%*d", width, value);

From Lee’s comment:
You can also use a * for the precision:

printf("%*.*f", width, precision, value);

Note that both width and precision must have type int as expected by printf for the * arguments, type size_t is inappropriate as it may have a different size and representation on the target platform.

Leave a Comment