Introduction
Announcements

Schedule
Labs
Assignments
TA office hours

Tests, exam

Topic videos
Some course notes
Extra problems
Lecture recordings

Discussion board

Grades so far

The return type of getc() and getchar()

The return type of getc() and getchar() is int, not char, and it's important to understand why this is.

On most systems, chars are eight bits, and I'll assume that in the rest of this page for simplicity. But the same principles apply to any size chars.

If chars are eight bits, there are 256 possible values. We can number them from 0 to 255, or we can number them from −128 to 127.

The return value from getc() or getchar() can be any one of these char values. It can also be −1 to indicate end-of-file.

Therefore there are 257 possible return values from getc(). (I'm going to stop saying "or getchar()".)

But a char can only hold 256 different possible values. So the return value from getc() can't fit in a char.

So the return type of getc() is int. And with the further guarantee that the character return values are from 0 to 255, never negative; so −1 can be used for EOF. If you convert this return value to a char, then you are going to restrict those 257 different possible return values to 256 different possible values. This means that two of them become the same. Generally that will be −1 and 255. So either you will mistakenly treat 255 as EOF, or you will never see EOF, depending on whether chars are considered to go from −128 to 127 or from 0 to 255 on your particular system.

One common way in which people accidentally convert the return value of getc() to char is like this:

        char c;
        while ((c = getc(fp)) != EOF)
            ...

An assignment is a conversion to that type.

The fix to the above code is to declare c to be of type int instead of char.

Remember that this doesn't stop you from subsequently assigning c to a char variable (such as a member of an array of chars), nor passing it to putc(), etc, AFTER you have checked that it is not EOF. Chars are just small integers in C.