Q: How can I implement a variable field width with printf? That is, instead of something like %8d, I want the width to be specified at run time.
A: printf("%*d", width, x) will do just what you want. The asterisk in the format specifier indicates that an int value from the argument list will be used for the field width. (Note that in the argument list, the width precedes the value to be printed.) See also question 12.15.
References:
K&R1 Sec. 7.3
K&R2 Sec. 7.2
ISO Sec. 7.9.6.1
H&S Sec. 15.11.6
CT&P Sec. A.1