Quoting from Kernighan and Ritchie’s ‘The C Programming Language’ Page 16 –
#include<stdio.h>
main()
{
int c;
c = getchar();
while(c!=EOF)
{
putchar(c);
c = getchar();
}
getchar();
return 0;
}
“The type
charis specifically meant for storing such character
data, but any integer type can be used. We usedintfor a subtle but
important reason. The problem is distinguishing the end of the input
from valid data. The solution is thatgetcharreturns a distinctive
value when there is no more input, a value that cannot be confused
with any real character. This value is calledEOF, for “end of
file”. We must declarecto be a type big enough to hold any value
thatgetcharreturns. We can’t usecharsincecmust be big
enough to holdEOFin addition to any possiblechar. Therefore we
useint.”.
I looked up in stdio.h, it says #define EOF (-1)
The book conclusively states that char cannot be used whereas this program “works just fine” (See EDIT) with c as char data type as well. What is going on? Can anyone explain in terms of bits and signed values?
EDIT:
As Oli mentioned in the answer, the program cannot distinguish between EOF and 255. So it will not work fine. I want to know what’s happening – Are you saying that when we do the comparison c!=EOF, the EOF value gets cast to a char value = 255 (11111111 in binary; i.e. the bits 0 through 7 of EOF when written in 2’s complement notation)?
Your program doesn’t work fine; it won’t be able to distinguish between
EOFand255.The reason it appears to work correctly is because
charis probablysignedon your platform, so it’s still capable of representing-1.