当不能用char表示-1时,为什么将EOF定义为-1? [英] Why is EOF defined to be −1 when −1 cannot be represented in a char?

查看:229
本文介绍了当不能用char表示-1时,为什么将EOF定义为-1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在树莓派上学习C编程,但是我发现我的程序永远无法成功捕获EOF。我使用 char c = 0; printf(%d,c-1); 测试 char 类型,发现 char 类型范围为0到255,为 unsigned short 。但 stdio.h 中定义的 EOF 为(-1)。那我的Pi上安装了错误的cc包吗?我该如何解决?如果我手动更改了 stdio.h 中的 EOF 值,还会有其他问题吗?

I'm learning the C programming on a raspberry pi, however I found that my program never catches the EOF successfully. I use char c=0; printf("%d",c-1); to test the char type, finding that the char type ranges from 0 to 255, as an unsigned short. but the EOF defined in stdio.h is (-1). So is the wrong cc package installed on my Pi? how can I fix it? If I changed the EOF value in stdio.h manually, will there be further problems?

让我担心的是,当我从K& R书中学习时,有一些示例使用诸如的代码((c = getchar())!= EOF),我在Ubuntu机器上遵循了该命令,并且工作正常。我只是想知道现代C语言实践是否放弃了这种语法,或者我的Raspberry Pi中存在冲突?

what worries me is that ,when I learning from the K&R book, there are examples which use code like while ((c=getchar())!=EOF), I followed that on my Ubuntu machine and it works fine. I just wonder if such kind of syntax is abandoned by modern C practice or there is something conflict in my Raspberry Pi?

这是我的代码:

#include <stdio.h>
int main( void )
{
        char c;
        int i=0;
        while ((c=getchar())!=EOF&&i<50) {
                putchar(c);
                i++;
        }
        if (c==EOF)
                printf("\nEOF got.\n");
        while ((c=getchar())!=EOF&&i<500) {
                printf("%d",c);
                i++;
        }
}

即使我将输入重定向到文件,它一直在屏幕上打印255,从不终止该程序。

even when I redirect the input to an file, it keeps printing 255 on the screen, never terminate this program.

最后,我发现我错了,在K& ; R book,它将c定义为int而不是char。问题已解决。

Finally I found that I'm wrong,In the K&R book, it defined c as an int, not a char. Problem solved.

推荐答案

您需要存储 fgetc() getchar()等在 int 中,因此您可以捕获 EOF 。这是众所周知的,并且到处都是这种情况。 EOF 必须与所有适当的字符区分开,因此决定像 fgetc()这样的函数将有效字符返回为non -负值(即使已签名 char )。文件结束条件由 -1 表示,它为负,因此不能与任何有效字符 fgetc()可能会返回。

You need to store the character read by fgetc(), getchar(), etc. in an int so you can catch the EOF. This is well-known and has always been the case everywhere. EOF must be distinguishable from all proper characters, so it was decided that functions like fgetc() return valid characters as non-negative values (even if char is signed). An end-of-file condition is signalled by -1, which is negative and thus cannot collide with any valid character fgetc() could return.

请勿编辑系统头文件,尤其不要更改此处定义的常量的值。如果这样做,则会破坏这些标题。请注意,即使您更改标题中的 EOF 值,也不会更改 fgetc()返回文件结尾或错误,这只会使 EOF 的值错误。

Do not edit the system headers and especially do not change the value of constants defined there. If you do that, you break these headers. Notice that even if you change the value of EOF in the headers, this won't change the value functions like fgetc() return on end-of-file or error, it just makes EOF have the wrong value.

这篇关于当不能用char表示-1时,为什么将EOF定义为-1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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