title: Why is EOF defined to be −1 when −1 cannot be represented in a char?
body: <p>I'm learning the C programming on a raspberry pi, however I found that my program never catches the EOF successfully. I use <code>char c=0; printf("%d",c-1);</code> to test the <code>char</code> type, finding that the <code>char</code> type ranges from 0 to 255, as an <code>unsigned short</code>. but the <code>EOF</code> defined in <code>stdio.h</code> is (-1). So is the wrong cc package installed on my Pi? how can I fix it? If I changed the <code>EOF</code> value in <code>stdio.h</code> manually, will there be further problems?</p>
  
  <hr>
  
  <p>what worries me is that ,when I learning from the K&amp;R book, there are examples which use code like <code>while ((c=getchar())!=EOF)</code>, I followed that on my Ubuntu machine and it works fine. I just wonder if such kind of syntax is abandoned by modern C practice or there is something conflict in my Raspberry Pi?</p>
  
  <p>here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int main( void )
  {
          char c;
          int i=0;
          while ((c=getchar())!=EOF&amp;&amp;i&lt;50) {
                  putchar(c);
                  i++;
          }
          if (c==EOF)
                  printf("\nEOF got.\n");
          while ((c=getchar())!=EOF&amp;&amp;i&lt;500) {
                  printf("%d",c);
                  i++;
          }
  }
  </code></pre>
  
  <p>even when I redirect the input to an file, it keeps printing 255 on the screen, never terminate this program.</p>
  
  <hr>
  
  <p>Finally I found that I'm wrong,In the K&amp;R book, it defined c as an int, not a  char. Problem solved.</p>