The EOF character of C

本文探讨了在C编程中EOF的定义及其在stdin中的使用。通过展示一个实例程序,说明如何正确捕获EOF并处理它。文章还强调了使用整数存储EOF的重要性,因为其二进制表示可能超出0x00~0xFF范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

EOF is defined in stdio.h and you should not assume it to be of any particular value.


a program used to show how to catch  EOF from stdin:

(from C Primer Plus)

int main(void) {
    int ch;
    while((ch = getchar()) != EOF)
        putchar(ch);
}

It uses an int to contain the value of EOF because the binary representation of EOF can be out of the range of 0x00~0xFF, such as 0x025c. 

If you use a char to catch a EOF, all you can get is 0x5c, printed as character '\\'


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值