为什么我不能找到EOF在C中的价值? [英] Why can't I find the value of EOF in C?

查看:106
本文介绍了为什么我不能找到EOF在C中的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读这本书C程序设计语言,并有是要求确认练习的前pression 的getchar()!= EOF 将返回1或0。现在原来的code之前,有人问我这样做是:

I'm reading the book "The C Programming Language" and there is an exercise that asked to verify that the expression getchar() != EOF is returning 1 or 0. Now the original code before I was asked to do that was:

int main()
{
    int c;
    c = getchar();

    while (c != EOF)
    {
        putchar(c);
        c = getchar();
    }  
}

所以我想将其更改为:

So I thought changing it to:

int main()
{
    int c;
    c = getchar();

    while (c != EOF)
    {
        printf("the value of EOF is: %d", c);
        printf(", and the char you typed was: ");

        putchar(c);
        c = getchar();
    }
}

和书中的答案是:

int main()
{
  printf("Press a key\n\n");
  printf("The expression getchar() != EOF evaluates to %d\n", getchar() != EOF);
}

能否请你给我解释为什么我的路没有工作?

Could you please explain to me why my way didn't work?

推荐答案

由于如果 C EOF 中, ,而循环结束(或者甚至不会启动,如果它已经 EOF 是输入的第一个字符)。运行循环的另一次迭代的条件是 C 不会 EOF

Because if c is EOF, the while loop terminates (or won't even start, if it is already EOF on the first character typed). The condition for running another iteration of the loop is that c is NOT EOF.

这篇关于为什么我不能找到EOF在C中的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆