Input and getchar

The most basic function to read input in C is int getchar(void). Obviously, it reads one char. What might not be obvious (at least not initially) is that we should not save the returned value in a variable of type char. Instead, do as follows:

int c;
c = getchar();

Suppose you wish to read all input and use getchar() in a loop. When all input has been read, the return value from getchar() is the special value EOF which is an integer value that is guaranteed to be different from the character set used (such as ASCII). EOF stands for enf of file and its value can be -1 for instance.

Writing:

while ((c = getchar()) != EOF) {

}

Will end up in an infinite loop if the variable c has an unsigned integer type, such as unsigned char. In C we have three distinct char types:

  • signed char,
  • unsigned char, and
  • char
  • .

The last of these is either signed or unsigned and which it is is implementation defined (decided by the compiler). If it is unsigned, then c will never be equal to EOF and the loop will not terminate.

Therefore, it is best to declare c as above with type int. For the assignments in the book with downloadable files, we explicitly tell the compiler that we want char to be unsigned in order to test your code for this trap.

Leave a Reply

Your email address will not be published. Required fields are marked *