Set character width of number in C

Published on September 18th, 2019

c icon

I have recently found out that you can set width of number in C. This can be really useful for aligning numbers if they don’t have same width. All you need to do is add number of characters between % and d (for int) in printf statement.

Here is working example.

#include <stdio.h>

int main () {
  int oneNumber = 31415, otherNumber = 24;

  printf("%5d\n", oneNumber);
  printf("%5d\n", otherNumber);

  return 0;
}

Which will produce this output.

./a.out
31415
   24