Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The prototype for <code>getchar</code> is:</p> <pre><code>int getchar (void); </code></pre> <p>It returns an <code>int</code> because it needs to return EOF which is not a character.</p> <hr> <p>Expanding on that, the function needs to be able to return <em>any</em> valid character, as well as being able to return a special "character" indicating that you're reached the end of file (or an error has occurred).</p> <p>Since a <code>char</code> data type can only hold one of the characters, there would be no way to indicate this end of file if all the function returned was a <code>char</code>. That's because, if you selected one of those <code>char</code> values to represent end of file, you would not be able to tell the difference between the end of file and the actual character.</p> <p>For example, let's choose 'A' to indicate end of file. When you actually get back an 'A' from <code>getchar()</code>, how will you know whether you've reached the end of file or whether the user actually entered 'A'?</p> <p>To get around this, many functions that give you back a <code>char</code> will actually give you an <code>int</code> and use one of the integer values which don't have an actual <code>char</code> equivalent (such as -1).</p> <p>In that case (assuming an 8-bit <code>char</code>), you would get back either a -1 if you've reached the end of the file or a value from 0 through 255 inclusive representing the character entered.</p> <hr> <p>And you need to get yourself both a more modern book <em>and</em> a more modern compiler:</p> <pre><code>#include &lt;stdio.h&gt; int main (void) { int ch = getchar(); if (ch != EOF) printf("You typed: %c\n", ch); return 0; } </code></pre> <p>Why anyone is still using Borland is this day and age (where gcc is both just as cheap and <em>much</em> better) is beyond me.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload