Listing 1

/*  show.c - Demonstrates some character
   printing functions */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define showx(a) printf(isprint(a) ? \
   "%c": "\\x%\.2x", a)

void
showc( int c )
{
   static char esc[ ] = "\a\b\t\n\r\f\v";
   static char *ptr[ ] = {
   "\\a", "\\b", "\\t", "\\n",
   " \\r ", " \\ f ", "\\v"
   };
   char *s = strchr(esc, c);
   
   if (s && c)
      printf ( ptr[s-esc] );
   else
      showx( c );
}

void
showasciic(int c)
{
   static char *a[ ] = {
      "<NUL>", "<SOH>", "<STX>", "<ETX>",
      "<EOT>", "<ENQ>", "<ACK>", "<BEL>",
      "<BS>", "<HT>", "<LF>", "<VT>",
      "<FF>", "<CR>" "<SO>", "<SI>",
      "<DLE>", "<DC1>", "<DC2>", "<DC3>",
      "<DC4>", "<NAK>", "<STN>", "<ETB>",
      "<CAN>", "<EM>", "<SUB>", "<ESC>",
      "<FS>", "<GS>", "<RS>", "<US>"
   };
 
   static char b[ ] =
      " !\"#$%&'( )*+,-./0123456789:;<=>?" \
      "@ABCDEFGHIJKLMNOPQRSTUVWXYZ [\\]^" \
      "'abcdefghijklmnopqrstuvwxyz{|}~";

   if (c & ~0x7f) ( /* not ASCII */
      printf ("<");
      showx(c);
      printf ("?>");
   }
   else { /* ASCII */
   if {c == 0x7f)/* DEL */
      printf ("<DEL>");
   else if (c & ~0xlf) /* graphic */
      putchar(b[c-32]);
   else /* control */
      printf (a[a]);
   }
}

void
main(void)
{
   int c;
   
   for (c = -1; c < 129; c++)
         showc (c);
   putchar ('\n');
   
   for (c=-1; c<129; c++)
      showascii(c);
   putchar { ' \n ' );
}