title: End of File (EOF) in C
body: <p>I am currently reading the book C Programming Language by Ritchie &amp; Kernighan. And I am pretty confused about the usage of EOF in the <code>getchar()</code> function. </p>
  
  <p>First, I want to know why the value of EOF is -1 and why the value of <code>getchar()!=EOF</code> is 0. Pardon me for my question but I really don't understand. I really tried but I can't. </p>
  
  <p>Then I tried to run the example on the book that can count the number of characters using the code below but it seems that I never get out of the loop even if I press enter so I am wondering when would I reach the EOF?</p>
  
  <pre><code>main(){
     long nc;
     nc = 0;
     while (getchar() != EOF)
         ++nc;
     printf("%ld\n", nc);
  }
  </code></pre>
  
  <p>Then, I read the same problem at <a href="https://stackoverflow.com/questions/1622092/problem-with-eof-in-c">Problem with EOF in C</a>. Most people advised that instead of using EOF, use the terminator \n or the null terminator '\0' which makes a lot of sense.</p>
  
  <p>Does it mean that the example on the book serves another purpose? </p>