C Programming/stdio.h/getchar

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  function prototype is

Sample usage
The following program uses  to read characters into an array and print them out using the putchar function after an end-of-file character is found.

The program specifies the reading length's maximum value at 1000 characters. It will stop reading either after reading 1000 characters or after reading in an end-of-file indicator, whichever comes first.

Common mistake
A common mistake when using  is to assign the result to a variable of type   before comparing it to EOF.

The following example shows this mistake:

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

Consider a system in which s are 8 bits wide, representing 256 different values. 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 's result is assigned to a , 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, 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  type is unsigned, then the opposite effect occurs. Because EOF is negative, it can never be equal to any unsigned, 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.

On systems where  and   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  returns EOF. If  indicates that end-of-file has not been reached, and   indicates that no errors have occurred, then the EOF returned by   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 " systems.