I am trying to write a program that displays the numerical value of certain character constants (those contained in if ). The code works except for one problem. It is assumed that the output should be well-aligned in columns, but as shown below, this is not the case. What is the best way to align columns properly?
Here is 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 conclusion:
