这是什么的getchar()究竟做什么? [英] What does getchar() exactly do?

查看:411
本文介绍了这是什么的getchar()究竟做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读K&安培; R,我这个程序是如何工作的困惑

I am reading K&R and I am baffled by how this program works

#include<stdio.h>

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

当我们输入一个字符串,它赋予了相同的输出。我有2个疑惑 -

When we enter a string, it gives the same output. I have 2 doubts -

它是如何输出整个字符串?我希望它读取一个字符和一个输入再问。

How does it output the whole string? I would expect it to read one character and ask again for an input.

我们是进入由EOF结束所有字符串?

Are all strings we enter terminated by an EOF?

推荐答案

在你很可能使用的是简单的设置,的getchar 适用于的缓存的输入,所以你必须preSS输入的getchar得到任何读取之前。字符串不是由 EOF 终止;事实上, EOF 是不是一个真正的人物,而是一个神奇的值,指示文件的末尾。但 EOF 是看不懂的字符串的一部分。这是什么的getchar 返回时,有没有什么东西可看。

In the simple setup you are likely using, getchar works with buffered input, so you have to press enter before getchar gets anything to read. Strings are not terminated by EOF; in fact, EOF is not really a character, but a magic value that indicates the end of the file. But EOF is not part of the string read. It's what getchar returns when there is nothing left to read.

这篇关于这是什么的getchar()究竟做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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