home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_01 / 9n01135a < prev    next >
Text File  |  1990-12-16  |  1KB  |  74 lines

  1.  
  2.  
  3. /* show.c -- Demonstrates some character
  4.     printing functions */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9.  
  10. #define showx(a) printf(isprint(a) ? \
  11.     "%c": "\\x%\.2x", a)
  12.  
  13. void
  14. showc( int c )
  15. {
  16.     static char esc[ ] = "\a\b\t\n\r\f\v";
  17.     static char *ptr[ ] = {
  18.     "\\a", "\\b", "\\t", "\\n",
  19.     " \\r ", " \\ f ", " \\v "
  20.     };
  21.     char *s = strchr(esc, c);
  22.  
  23.     if (s && c)
  24.         printf ( ptr[s-esc] );
  25.     else
  26.         showx( c );
  27. }
  28.  
  29. void
  30. showasciic(int c)
  31. {
  32.     static char *a[ ] = {
  33.         "<NUL>", "<SOH>", "<STX>", "<ETX>",
  34.         "<EOT>", "<ENQ>", "<ACK>", "<BEL>",
  35.         "<BS>", "<HT>", "<LF>", "<VT>",
  36.         "<FF>", "<CR>", "<SO>", "<SI>",
  37.         "<DLE>", "<DCl>", "<DC2>", "<DC3>",
  38.         "<DC4>", "<NAK>", "<STN>", "<ETB>",
  39.         "<CAN>", "<EM>", "<SUB>", "<ESC>",
  40.         "<FS>", "<GS>", "<RS>", "<US>"
  41.     };
  42.     static char b[ ] =
  43.         " !\"#$%&'( )*+,-./0123456789:;<=>?" \
  44.         "@ABCDEFGHIJKLMNOPQRSTUVWXYZ [\\]^" \
  45.         "'abcdefghijklmnopqrstuvwxyz{|}~";
  46.  
  47.     if (c & ~Ox7f) {    /* not ASCII */
  48.         printf ("<");
  49.         showx(c);
  50.         printf ("?>");
  51.     }
  52.     else {    /* ASCII */
  53.     if (c == Ox7f)    /* DEL */
  54.         printf ("<DEL>");
  55.     else if (c & ~Oxlf)    /* graphic */
  56.         putchar(b[c-32]);
  57.     else    /* control */
  58.         printf (a[c]);
  59.     }
  60. }
  61.  
  62. void
  63. main(void)
  64. {
  65.     int c;
  66.     for (c = -1; c < 129; c++) 
  67.            showc (c);
  68.     putchar ('\n');
  69.     for (c=-1; c<129; c++)
  70.         showascii(c);
  71.     putchar ( ' \n ' );
  72. }
  73.  
  74.