Formatting spacing with the printf function C

Viewed 534

How can I create a code where the spacing of the of the Numbers and Months is equidistant from one another. The code source code has been gotten in the answer of issue.

#include <stdio.h>

#define MAX_LEN 32

int main() {
    int Numbers[12] = {3, 4, 5, 1, 2, 7, 8, 9, 3, 4, 7, 8};
    char Months[][MAX_LEN] = {"Jan",       "Feb",     "March",    "April",
                              "May",       "June",    "July",     "August",
                              "September", "October", "November", "December"};

    for (int i = 0; i < 12; i++)
        printf("%s: %d\n", Months[i], Numbers[i]);

    return 0;
}

Output

Jan: 3
Feb: 4
March: 5
April: 1
May: 2
June: 7
July: 8
August: 9
September: 3
October: 4
November: 7
December: 8

Expected Output:

Jan:       3
Feb:       4
March:     5
April:     1
May:       2
June:      7
July:      8
August:    9
September: 3
October:   4
November:  7
December:  8
2 Answers

You can use extra modifiers in your format string to make printf() align things correctly:

  • To print something of a particular width prepend the the width as an integer before the formatting character: e.g., %10s will print a string to a (maximum) column of width 10, padding to the left with spaces. If your string is longer than 10 characters, it prints the full string though so bear this in mind. So your particular example:
char Months[][MAX_LEN] = {"Jan",       "Feb",     "March",    "April",
                          "May",       "June",    "July",     "August",
                          "September", "October", "November", "December"};

for (int i = 0; i < 12; i++) {
  /* Note the 10 here! */
  printf("%10s: %d\n", Months[i], Numbers[i]);       
}

Results in

       Jan: 3
       Feb: 4
     March: 5
     April: 1
       May: 2
      June: 7
      July: 8
    August: 9
 September: 3
   October: 4
  November: 7
  December: 8

If you want to left-justify your strings, you simple prepend a - before the integer value, i.e.:

printf("%-10s: %d\n", Months[i], Numbers[i]);

Which produces

Jan       : 3
Feb       : 4
March     : 5
April     : 1
May       : 2
June      : 7
July      : 8
August    : 9
September : 3
October   : 4
November  : 7
December  : 8
  • Alternatively you can also use the * which allows you to state the length to pad to variably in the printf() call. To use it you calculate the length you wish to pad to as an extra argument:
int myPadWidth = 15;
/* calculate the length of Months[i] each loop and cast size_t result to int */
int lenMonths = (int)strlen(Months[i]);
/* Note the extra argument in the printf call here! */
printf("%s: %*d\n",Months[i],myPadWidth-lenMonths,Numbers[i]);

Which would result in

Jan:            3
Feb:            4
March:          5
April:          1
May:            2
June:           7
July:           8
August:         9
September:      3
October:        4
November:       7
December:       8

There are a number of ways to achieve this.

One is to use a * character as the (minimum) field width specifier in the printf format string; this specifies that the actual value be determined, when the function is called, from an extra argument, given immediately before the value to which it being applied.

The value of that width specifier can be calculated by subtracting from the (maximum) desired field width the length of the preceding field; in your case, that will be the length of the Months[i] string, which is returned by the strlen function.

Here's a working example:

#include <stdio.h>
#include <string.h> // For the definition of "strlen"

#define MAX_LEN 32

int main()
{
    int Numbers[12] = { 3, 4, 5, 1, 2, 7, 8, 9, 3, 4, 7, 8 };
    char Months[][MAX_LEN] = { "Jan",       "Feb",     "March",    "April",
                               "May",       "June",    "July",     "August",
                               "September", "October", "November", "December" };

    for (int i = 0; i < 12; i++)
        printf("%s: %*d\n", Months[i], 10 - (int)strlen(Months[i]), Numbers[i]);

    return 0;
}

Output:

Jan:       3
Feb:       4
March:     5
April:     1
May:       2
June:      7
July:      8
August:    9
September: 3
October:   4
November:  7
December:  8
Related