home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / TABTRICK.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  58 lines

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