Here is a code fragment from "The C Programming Language" by Kernighan and Ritchie.
#include <stdio.h>
/* copy input to output; 2nd version */
main()
{
int c;
while ((c = getchar()) != EOF) {
putchar(c);
}
Justification for using int c
instead of char c
:
...We can't use char since c must be big enough to hold EOF in addition to any possible char.
I think using int instead of char is only justifiable if c is modified with unsigned because an signed char won't be able to hold
the value of EOF which is -1, and when I wrote this program char c
was interpreted as signed char c
, therefore I had no problem.
Were char variables previously unsigned by default? And if so, then why did they alter it?