Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning columns in C output

Tags:

c

I'm trying to write a program that displays the numerical value of certain characters constants (the ones in the if statements). The code works, except for one problem. The output is supposed to be nicely aligned in columns, but as shown below, it isn't. What is the best way to get the columns to line up properly?

Here's my code:

#include <stdio.h>
#include <ctype.h>

int main() {

    unsigned char c;

    printf("%3s %9s %12s %12s\n", "Char", "Constant", "Description", "Value");

    for(c=0; c<= 127; ++c){

        if (c == '\n') {
            printf("%3d %7s \t%s \t\t%s%03x\n", c,"\\n","newline","0x", c);

        }else if (c == '\t'){
            printf("%3d %7s \t%s \t\t%s%03x\n", c,"\\t","horizontal tab","0x", c);

        }else if (c == '\v'){
            printf("%3d %7s \t%s \t\t%s%03x\n", c,"\\v","vertical tab","0x", c);

        }else if (c == '\b'){
            printf("%3d %7s \t%s \t\t%s%03x\n", c,"\\b","backspace","0x", c);

        }else if (c == '\r'){
            printf("%3d %7s \t%s \t\t%s%03x\n", c,"\\r","carriage return","0x", c);

        }else if (c == '\f'){
            printf("%3d %7s \t%s \t\t%s%03x\n", c,"\\f","form feed","0x", c);

        }else if (c == '\\'){
            printf("%3d %7s \t%s \t\t%s%03x\n", c,"\\","backslash","0x", c);

        }else if (c == '\''){
            printf("%3d %7s \t%s \t\t%s%03x\n", c,"\'","single quote","0x", c);

        }else if (c == '\"'){
            printf("%3d %7s \t%s \t\t%s%03x\n", c,"\"","double quote","0x", c);

        }else if (c == '\0'){
            printf("%3d %7s \t%s \t\t%s%03x\n", c,"\\0","null","0x", c);
        }
    }


    return 0;
}

Here's the output:

Output

like image 587
Tucker Beauchamp Avatar asked Feb 11 '16 01:02

Tucker Beauchamp


People also ask

How will you align the output left or right in C programming?

By using justifications in printf statement we can arrange the data in any format. To implement the right justification, insert a minus sign before the width value in the %s character.

How do you align columns?

On the Home tab, click Paragraph, and then click Align. Select the Align with option and then select the paragraph tag pertaining to the column one paragraph.

How do I make columns in printf?

The code to generate this would look like: printf("Column1 Column2 Column3\n"); printf("%7d%11d%10d\n", 100, 1221, 9348);

How should columns of numbers be aligned?

Always right-align headings and data in columns so that units, tens, hundreds and thousands are aligned and numbers of equal value are easily comparable.


1 Answers

Using \t leaves you at the mercy of your output device. Instead you could use minimum field widths for the strings, e.g. %-20s will print at least 20 characters, right-padding with spaces.

%-20.20s will truncate the string if it was longer; %-20s will bump everything else over to the right. The - means to left-justify (default is right-justify)


To avoid code duplication you could use a helper function, e.g.:

void print_item(char code, char const *abbrev, char const *description)
{
     printf("%3d %7s %20.20s %#03x\n", code, abbrev, description, (unsigned char)code);
}

// ... in your function
if (c == '\n')
     print_item(c, "\\n", "newline");

I modified the printf format string:

  • Using %20.20s as suggested above
  • %#03x, the # means it will prepend 0x for you
  • (unsigned char)code for the last one means it will behave nicely if you passed any negative chars in. (Typically chars range in value from -128 to 127).
like image 200
M.M Avatar answered Oct 20 '22 05:10

M.M