How to make columns in C

Borrowed heavily from Keith Trnka's guide

Correction: this isn't always the best way to setup columns.

There are two starting points for making columns: you can either choose a column header and then make columns, or choose column widths and then make a column header. The example below details the way of making a column header first and then deciding your column widths. Hopefully, both ways can be clear by the end of this webpage.

Example

Suppose that this is the output you want:

Column1    Column2   Column3
    100       1221      9348
     23        211       214

Now, look at each character. Spaces are shown as underscores (_) below.
C o l u m n 1 _ _ _ _ C o l u m n 2 _ _ _ C o l u m n 3
_ _ _ _ 1 0 0 _ _ _ _ _ _ _ 1 2 2 1 _ _ _ _ _ _ 9 3 4 8
_ _ _ _ _ 2 3 _ _ _ _ _ _ _ _ 2 1 1 _ _ _ _ _ _ _ 2 1 4

Each column is color-coded so you can see that column one is 7 characters wide, column two is 11 characters wide, and column three is 10 characters wide.

The code to generate this would look like:

printf("Column1    Column2   Column3\n");
printf("%7d%11d%10d\n", 100, 1221, 9348);
printf("%7d%11d%10d\n", 23, 211, 214);

Notice that the %7d goes with values from the first column, %11d goes with values from the second column, and %10d goes with values from the third column. The reason d is used is because we're using integers. If you were writing your own code, you'd probably have variable names instead of the numbers there.

How would this work for float or double?

Suppose that I change the numbers slightly so that they have decimal places:

Column1    Column2   Column3
  100.1       1221  9348.012
    2.3        211   214.000

So there's one decimal place in the first column, none in the second, and three in the third.

Then I would have code that looks like this:

printf("Column1    Column2   Column3\n");
printf("%7.1lf%11.0lf%10.3lf\n", 100.1, 1221.0, 9348.012);
printf("%7.1lf%11.0lf%10.3lf\n", 2.3, 211.0, 214.0);

It's important to note that the values in the printf statement have to match the data type in the string in the first part of the printf statement. So if you're using variables of type double, use lf. Even more important to note is that the numbers 7, 11, and 10 didn't change.

How could I start by deciding column widths?

First, you have to make sure to choose column widths that are at least as big as your column names. The columns should include at least one space to the left of each column name, except the first one, so adjust the widths accordingly. For instance, in the previous example, the column names are "Column1", "Column2", and "Column3". Each column requires 7 characters for the name alone. We need at least on space between the names, for the header "Column1 Column2 Column3". That means our column widths are 7, 8, and 8. With these column widths, a printf statement to show one row might look like:

printf("%7d%8d%8d", 1, 2, 3);

How can I do left-aligned columns?

For left-alignment, the same basic method works. There are two differences:
  1. Use %-7d instead of %7d
    You have to place a minus sign before the column width, after the percent sign. This does left-alignment.
  2. When deciding which characters go in which columns, put spaces in the column to the left.
    In the above colored diagram, the spaces to the left of the column 2 name are in column 2. For left-alignment, those 4 spaces go in column 1.