home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap06 / static.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-05  |  864 b   |  34 lines

  1. /* static.c -- demonstrates a static variable */
  2. /*             that holds count of lines,     */
  3. /*             words, and characters          */
  4.  
  5. main()
  6.     {
  7.     void countline();
  8.     printf("Type some lines of text.\n");
  9.     printf("Start a line with a . to quit.\n\n");
  10.  
  11.     while (getche() != '.')
  12.         countline();  /* accumulate word and */
  13.                       /* line counts         */
  14.     }
  15.  
  16. void countline()
  17.     {
  18.     static int words = 0; /* static variables */
  19.     static int chars = 0;
  20.     char ch;
  21.     ++chars; /* count char typed when */
  22.              /* function was called   */
  23.  
  24.     while ((ch = getche()) != '\r')
  25.         {
  26.         ++chars;
  27.         if (ch == ' ')
  28.             ++words;
  29.         }
  30.         ++words; /* count last word */
  31.  
  32.     printf("\nWords so far %d. Chars. so far %d\n", words, chars);
  33.     }
  34.