title: Why printf() after the while loop would not work?
body: <p>I'm a beginner and I've recently started learning C and here is an example in &quot;<em>The C Programming Language by Brian W. Kernighan, Dennis M. Ritchie</em>&quot; that I don't understand. This program is supposed to count new lines in input and print out the final result. This is the exact same program that is in the book (page 19). The output of it is nothing. I can input forever and it just goes to a new line...</p>
  <pre><code>main()
  {
      int c, nl;
  
      nl = 0;
      while ((c = getchar()) != EOF)
          if (c == '\n')
              ++nl;
      printf(&quot;%d\n&quot;, nl);
  }
  </code></pre>
  <p>If I put the &quot;printf(&quot;%d\n&quot;, nl)&quot; statement in the body of the if statement, the output would be printed each time on a new line and the value of &quot;nl&quot; wouldn't reset either. it just increments every time I input something and the program wouldn't terminate.</p>
  <p>Why doesn't the example work?</p>