Appendix R. ASCII Table

In a book of this sort it is traditional to have an appendix with an ASCII table. This book does not. Instead, here is a short C program that generates a complete ASCII table and writes it to the file ASCII.txt.


Example R-1. A C program to generate an ASCII table

   1 /*********************************************/
   2 /* ascii.c                                   */
   3 /* Generate ASCII table                      */
   4 /* To build: gcc -O2 ascii.c -o ascii-table  */
   5 /*                                           */
   6 /* This utterly trivial program written by   */
   7 /* Mendel Cooper, 04/07                      */
   8 /* I'm not proud of it, but it does the job. */
   9 /* License: Public Domain                    */
  10 /*********************************************/
  11 
  12 #include <stdio.h>
  13 
  14 #define MAX 255                /* FF hex       */
  15 #define FILENAME "ASCII.txt"   /* Outfile name */
  16 
  17 int main()
  18   {
  19   int i;
  20   FILE *fp;
  21 
  22   fp = fopen (FILENAME, "a" );
  23 
  24   for( i = 1; i <= MAX; i++ ) {
  25      fprintf( fp, "%5d  ", i );
  26      fputc( i, fp );
  27      fprintf( fp, "     " );
  28      if ( i % 5 == 0 )
  29         fprintf( fp, "\n" );
  30      }
  31 
  32      fprintf( fp, "\n" );
  33 
  34   return (0);
  35   } /* Outfile needs a bit of hand-editing for tidying up. */
  36 
  37 /* Try rewriting this as a shell script. */
  38 /* Not so easy, huh?                     */

To build (compile) the program: gcc -O2 ascii.c -o ascii-table