c Programming/C Reference/stdio.h/getchar

From Wikibooks, open books for an open world
Jump to: navigation, search

getchar is a function in C programming language that reads a single character from the standard input stream stdin, regardless of what it is, and returns it to the program. It is specified in ANSI-C and is the most basic input function in C. It is included in the stdio.h header file.

The getchar function prototype is[1]

int getchar(void);

Sample usage[edit]


 #include <stdio.h>
 int main(void)
   char str[1000];
   int ch, n = 0;
      str[n] = ch;
   return 0;


Common mistake[edit]

A common mistake when using getchar is to assign the result to a variable of type char before comparing it to EOF.[2]

The following example shows this mistake:

   char c;
   while ((c = getchar()) != EOF) { /* Bad! */

Consider a system in which chars are 8 bits wide, representing 256 different values. getchar may return any of the 256 possible characters, and it also may return EOF to indicate end-of-file, for a total of 257 different possible return values.

When getchar's result is assigned to a char, which can represent only 256 different values, there is necessarily some loss of information - when packing 257 items into 256 slots, there must be a collision. The EOF value, when converted to char, becomes indistinguishable from whichever one of the 256 characters shares its numerical value. If that character is found in the file, the above example will mistake it for an end-of-file indicator.

If the char type is unsigned, then the opposite effect occurs. Because EOF is negative, it can never be equal to any unsigned char, so the above example will not terminate at end-of-file. It will loop forever, repeatedly printing the character which results from converting EOF to a char.

On systems where int and char are the same size, even the "good" example in the previous section will suffer from the indistinguishability of EOF and some character's value. The proper way to handle this situation is to check feof and ferror after getchar returns EOF. If feof indicates that end-of-file has not been reached, and ferror indicates that no errors have occurred, then the EOF returned by getchar can be assumed to represent an actual character. These extra checks are rarely done, because most programmers assume that their code will never need to run on one of these "big char" systems.

See also[edit]


  1. http://www.phim.unibe.ch/comp_doc/c_manual/C/FUNCTIONS/getchar.html
  2. http://c-faq.com/stdio/getcharc.html