main(...">

Using int for character types when comparing with EOF

Quote from Kernighan and Ritchie "C Programming Language" Page 16 -

#include<stdio.h> main() { int c; c = getchar(); while(c!=EOF) { putchar(c); c = getchar(); } getchar(); return 0; } 

"The char type is specifically designed to store such character data, but any integer type can be used. We used int for a subtle but important reason. The problem is to distinguish the end of the input from valid data. The getchar solution returns a distinguishing value when there is no more input , the value of which can not be confused in any real character. This value is called EOF , for "end of file". We must declare c to be a type big enough to hold any value returned by getchar . We do not mo it use the char , since c must be big enough to hold EOF in addition to any possible char . Therefore we use int "..

I looked in stdio.h, it says #define EOF (-1)

The book finally says that char cannot be used, while this program "works fine" (see EDIT) with c as a char data type. What's happening? Can anyone explain in terms of bits and signed values?

EDIT:
As Oli said in the answer, the program cannot distinguish between EOF and 255 . So this will not work. I want to know what is going on. Are you saying that when we compare c! = EOF, the EOF value gets the value char = 255 (11111111 in binary terms, that is, bits 0 to 7 from EOF when they are written in 2 addition)?

+4
c
source share
2 answers

Your program is not working properly; he will not be able to distinguish between EOF and 255 .

The reason it works correctly is because char is probably signed on your platform, so it is still able to represent -1 .

+4
source share

getchar result is an input character converted to unsigned char and then to int or EOF , i.e. it will be in the range -1-255, which is 257 different values, you cannot put this in 8 bits of char without combining the two of them. In practice, either you will allow EOF as a valid character (this will happen if char not specified), or you will allow another character as EOF (this will happen if char will be signed).

Note. I am assuming an 8-bit char type, I know that this assumption is not supported by the standard, this is by far the most common implementation.

+8
source share

All Articles