There is a program to copy input to output as blow.
#include <stdio.h>
/* copy input to output */
int main()
{
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
}
}getchar and putchar are
the simplest. Each time it is called, getchar reads
the next
input character from
a text stream and returns that as its value. That is, after
c = getchar();
the variable c contains the next character of input. However, why is the type of c int rather than char?
The problem is distinguishing the end of the input from valid data. The solution is that getcharreturns
a distinctive value when there is no more input, a value that cannot be confused with any real character. This value is called EOF, for "end of file." EOF is an integer defined in<stdio.h>,
but the specific numeric value doesn‘t matter as long as it is not the same as anychar value.
By using the symbolic constant, we are assured that noting in the program depends on the specific numeric value. We must declare c to
be a type big enough to hold any value that getchar returns.
We can‘t use char since c must
be big enough to hold EOF in addition to any possible char.
Therefore we usr int.
Why do we use int rather than char
原文地址:http://blog.csdn.net/abnerwang2014/article/details/44408113