0

I know that I could use printf(%Nd, foo) if N is constant and I know it
But the problem is that N is in variable and calculated in program

I could do it with combining sprintf and printf:

sprintf(formatstr, "%%%dd", N);
printf(formatstr, foo);

But is there any cleaner way?

stek29
  • 384
  • 4
  • 14

1 Answers1

3

You can put a * in place of the field width. This means the next parameter will specify the width:

printf("%0*d", size, foo);
dbush
  • 186,650
  • 20
  • 189
  • 240