FreeStyle4 FreeStyle4 - 4 months ago 25
C Question

Printing number of spaces

Is there a way to specify the number of spaces using a variable?

For example lets say I have a basic print statement:

printf("%5d",someNumber);


This will print 5 spaces then the number. Is there a way that would let me declare a variable
var = 5
, and use a variable to determine spacing instead? The reason i'm asking is because I'm trying to control the number of spaces after each iteration of a loop to format data a certain way

Answer

Try:

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

You can find more info by man 3 printf