title: Aligning columns in C output
body: <p>I'm trying to write a program that displays the numerical value of certain characters constants (the ones in the <code>if</code> 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?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;ctype.h&gt;
  
  int main() {
  
      unsigned char c;
  
      printf("%3s %9s %12s %12s\n", "Char", "Constant", "Description", "Value");
  
      for(c=0; c&lt;= 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;
  }
  </code></pre>
  
  <p>Here's the output:</p>
  
  <p><a href="https://i.stack.imgur.com/6yEJQ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/6yEJQ.png" alt="Output"></a></p>