Advertisement
drankinatty

Generate Full ASCII-Chart in Single Loop

Apr 24th, 2018
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.49 KB | None | 0 0
  1. /* simple program to generate a full ASCII-chart on stdout */
  2. #include <stdio.h>
  3.  
  4. enum { TWO = 1u << 1, R1 = 1u << 5, R2 = 1u << 6, R3 = R1 + R2 };
  5.  
  6. int main (void) {
  7.  
  8.     char *special[] = { "(nul)", "(soh)", "(stx)", "(etx)",
  9.                         "(eot)", "(enq)", "(ack)", "(bel)",
  10.                         "(bs)",  "(ht)",  "(nl)",  "(vt)",
  11.                         "(np)",  "(cr)",  "(so)",  "(si)",
  12.                         "(dle)", "(dc1)", "(dc2)", "(dc3)",
  13.                         "(dc4)", "(nak)", "(syn)", "(etb)",
  14.                         "(can)", "(em)",  "(sub)", "(esc)",
  15.                         "(fs)",  "(gs)",  "(rs)",  "(us)",
  16.                         "(sp)",  "(del)" };
  17.  
  18.     /* print heading */
  19.     printf ("Char  Dec  Oct  Hex | Char  Dec  Oct  Hex | "
  20.             "Char  Dec  Oct  Hex | Char  Dec  Oct  Hex\n"
  21.             "--------------------------------------------"
  22.             "-----------------------------------------\n");
  23.  
  24.     /* print chars */
  25.     for (unsigned i = 0; i < R1; i++) {
  26.         char s1[TWO] = { (char)i+R1, 0 },
  27.              s2[TWO] = { (char)i+R2, 0 },
  28.              s3[TWO] = { (char)i+R3, 0 };
  29.  
  30.         printf ("%-5s %3d %04o 0x%02x | %-5s %3d %04o 0x%02x | "
  31.                 "%-5s %3d %04o 0x%02x | %-5s %3d %04o 0x%02x\n",
  32.                 special[i], i, i, i,
  33.                 i ? s1 : special[R1], i+R1, i+R1, i+R1,
  34.                 s2, i+R2, i+R2, i+R2,
  35.                 i == R1 - 1 ? special[R1+1] : s3, i+R3, i+R3, i+R3);
  36.     }
  37.  
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement