分类: C/C++
2011-04-25 21:00:07
C语言程序设计中有这么一段程序:
#include
/*用于将输入复制到输出的程序;*/
main()
{
int c;
while( (c = getchar()) != EOF)
putchar(c);
}
那么从键盘输入什么的时候字符C才等于EOF呢?
/* This program prompts for input, and then captures a character
* from the keyboard. If EOF is signalled (typically through a
* control-D or control-Z character, though not necessarily),
* the program prints 0. Otherwise, it prints 1.
*
* If your input stream is buffered (and it probably is), then
* you will need to press the ENTER key before the program will
* respond.
*/
#include
int main(void)
{
printf("Press a key. ENTER would be nice :-)\n\n");
printf("The expression getchar() != EOF evaluates to %d\n", getchar() != EOF);
return 0;
}
由以上可知输入control-D时,C == EOF!
那么EOF的值又是多少呢?
/*Write a program to print the value of EOF .*/
#include
int main(void)
{
printf("The value of EOF is %d\n\n", EOF);
return 0;
}
由以上程序结果可知EOF = -1 。