I'm new to C. From the book there is a sample code:
#include <stdio.h>
main() {
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}
The author writes a sentence like this:
We can't use char since c must be big enough to hold EOF in addition to any possible char. Therefore we use int.
Trying to understand, I modified the code like this:
#include <stdio.h>
main() {
char c=getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
if (c == EOF) {
putchar('*');
}
}
When I press Ctrl+D, *
was printed, which means c
holds EOF
, which confuses me. Can anybody explain a little bit about this?