我想了解的getchar()!= EOF [英] I'm trying to understand getchar() != EOF

查看:144
本文介绍了我想了解的getchar()!= EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读的C语言程序设计,至今已经明白了一切。
然而,当我穿过的getchar()和的putchar()来了,我不明白什么是他们的使用,更具体,有什么下面code一样。

I'm reading The C Programming Language and have understood everything so far. However when I came across the getchar() and putchar(), I failed to understand what is their use, and more specifically, what the following code does.

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

我理解的的main()的功能,整数声明的 C 的和的,而的循环。然而,即时通讯困惑的的,而的循环中的条件。什么是在这个C code中的输入,输出是什么。

I understand the main() function, the declaration of the integer c and the while loop. Yet im confused about the condition inside of the while loop. What is the input in this c code, and what is the output.

很抱歉,如果这是一个基本的和愚蠢的问题,但我只是在寻找一个简单的解释之前,我在书中继续前进,变得更加迷茫。

Sorry if this is a basic and stupid question, but I'm just looking for a simple explanation before I move on in the book and become more confused.

推荐答案

这code可以写成更清楚的:

This code can be written more clearly as:

main()
{
    int c;
    while (1) {
        c = getchar();            // Get one character from the input
        if (c == EOF) { break; }  // Exit the loop if we receive EOF ("end of file")
        putchar(c);               // Put the character to the output
    }
}

当没有更多的输入端接收到 EOF 字符。名称使得在正在从一个真正的文件读出的输入,而不是用户的输入(这是一个文件的一个特殊情况)的情况更有意义。

The EOF character is received when there is no more input. The name makes more sense in the case where the input is being read from a real file, rather than user input (which is a special case of a file).



[顺便说一句,一般函数应该写成 INT主要(无效)]

这篇关于我想了解的getchar()!= EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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