title: K&R Exercise 1.9 - program in infinite loop
body: <p>I am trying to complete exercise 1-9 in K&amp;R and I came up with this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  /* this program will trim each group of spaces down to a single space */
  int main()
  {
      int c, lastCharblank;
      lastCharblank = 0;
  
      while ((c = getchar()) != EOF) {
          if (c != ' ' || !lastCharblank)
              putchar(c);
  
          lastCharblank = (c == ' ');
      }
  
      putchar('\n');
  
      return 0;
  }
  </code></pre>
  
  <p>While running the program through the bash command line I am able to enter text("fix     these    spaces") and then I enter cntl-d to signal an EOF. The program returns the line with all the spaces corrected, but it does not exit. It seems to be in an infinite loop. Why doesn't it exit?</p>