home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNPD9404.ZIP / TABTRICK.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  2KB  |  55 lines

  1. /*
  2. **  TABTRICKs.C - Demonstrates how to use printf() for columnar formatting
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. #define putnum(i) putchar(i+'0')
  9.  
  10. void main()       /* void main() is not standard C, but avoids warnings */
  11. {
  12.       char *firstname[] = { "Aloysius",   "Bob",   "Dennis",  NULL },
  13.            *lastname[]  = { "Fuddrucker", "Stout", "Ritchie", NULL };
  14.       int   score[]     = { -10,          70,      200,       0    },
  15.             i, tabwidth;
  16.  
  17.       printf("%15sStudent Name%30s\n\n", "", "Test Score");
  18.       for (i = 0; NULL != lastname[i]; ++i)
  19.       {
  20.             tabwidth = 36                             /* spaces to tab  */
  21.                        -2                             /* allow for ", " */
  22.                        -strlen(lastname[i]);          /* lastname space */
  23.             printf("%15s%s, %-*s%3d\n",
  24.                   "", lastname[i], tabwidth, firstname[i], score[i]);
  25.       }
  26.  
  27.       /* print a ruler to make things clearer   */
  28.  
  29.       puts("");
  30.       for (i = 0; i < 71; ++i)
  31.       {
  32.             if (i == 10 * (i / 10))
  33.                   putnum(i / 10);
  34.             else  putchar(' ');
  35.       }
  36.       puts("");
  37.       for (i = 0; i < 71; ++i)
  38.             putnum(i % 10);
  39. }
  40.  
  41. /*
  42.  
  43. RESULTS:
  44.  
  45.                Student Name                    Test Score
  46.  
  47.                Fuddrucker, Aloysius                -10
  48.                Stout, Bob                           70
  49.                Ritchie, Dennis                     200
  50.  
  51. 0         1         2         3         4         5         6         7
  52. 01234567890123456789012345678901234567890123456789012345678901234567890
  53.  
  54. */
  55.