C语言 getchar() 如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3676796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does getchar() work?
提问by Shubham
I am confused by a program mentioned in K&R that uses getchar(). It gives the same output as the input string:
我对 K&R 中提到的使用getchar(). 它提供与输入字符串相同的输出:
#include <stdio.h>
main(){
int c;
c = getchar();
while(c != EOF){
putchar(c);
c = getchar();
}
}
Why does it print the whole string? I would expect it to read a character and ask again for the input.
为什么它打印整个字符串?我希望它读取一个字符并再次询问输入。
And, are all strings we enter terminated by an EOF?
而且,我们输入的所有字符串都以 EOF 结尾吗?
采纳答案by Erich Kitzmueller
In the simple setup you are likely using, getcharworks with bufferedinput, so you have to press enter before getchar gets anything to read. Strings are not terminated by EOF; in fact, EOFis not really a character, but a magic value that indicates the end of the file. But EOFis not part of the string read. It's what getcharreturns when there is nothing left to read.
在您可能使用的简单设置中,getchar使用缓冲输入,因此您必须在 getchar 读取任何内容之前按 Enter。字符串不以EOF;结尾 实际上,EOF它并不是真正的字符,而是一个表示文件结尾的魔法值。但EOF不是读取的字符串的一部分。getchar当没有任何东西可供阅读时,它就会返回。
回答by Timothy
There is an underlying buffer/stream that getchar() and friends read from. When you enter text, the text is stored in a buffer somewhere. getchar() can stream through it one character at a time. Each read returns the next character until it reaches the end of the buffer. The reason it's not asking you for subsequent characters is that it can fetch the next one from the buffer.
有一个 getchar() 和朋友从中读取的底层缓冲区/流。当您输入文本时,文本会存储在某个缓冲区中。getchar() 可以一次一个字符地通过它。每次读取都会返回下一个字符,直到它到达缓冲区的末尾。它不要求您输入后续字符的原因是它可以从缓冲区中获取下一个字符。
If you run your script and type directly into it, it will continue to prompt you for input until you press CTRL+D (end of file). If you call it like ./program < myInput where myInput is a text file with some data, it will get the EOF when it reaches the end of the input. EOF isn't a character that exists in the stream, but a sentinel value to indicate when the end of the input has been reached.
如果您运行脚本并直接在其中键入,它将继续提示您输入,直到您按 CTRL+D(文件结尾)。如果你像 ./program < myInput 这样调用它,其中 myInput 是一个带有一些数据的文本文件,当它到达输入的末尾时它会得到 EOF。EOF 不是流中存在的字符,而是指示何时到达输入末尾的标记值。
As an extra warning, I believe getchar() will also return EOF if it encounters an error, so you'll want to check ferror(). Example below (not tested, but you get the idea).
作为额外的警告,我相信 getchar() 在遇到错误时也会返回 EOF,因此您需要检查 ferror()。下面的示例(未经测试,但您明白了)。
main() {
int c;
do {
c = getchar();
if (c == EOF && ferror()) {
perror("getchar");
}
else {
putchar(c);
}
}
while(c != EOF);
}
回答by pmg
Strings, by Cdefinition, are terminated by '\0'. You have no "C strings"in your program.
根据C定义,字符串以'\0'. 你"C strings"的程序中没有。
Your program reads characters (buffered till ENTER) from the standard input (the keyboard) and writes them back to the standard output (the screen). It does this no matter how many characters you type or for how long you do this.
您的程序从标准输入(键盘)读取字符(缓冲到 ENTER)并将它们写回标准输出(屏幕)。无论您输入多少个字符或输入多长时间,它都会执行此操作。
To stop the program you have to indicate that the standard input has no more data (huh?? how can a keyboard have no more data?).
要停止程序,您必须指出标准输入没有更多数据(哈??键盘怎么可能没有更多数据?)。
You simply type Ctrl+D(Unix) or Ctrl+Z(Windows) to pretend the file has reached its end.Ctrl+D(or Ctrl+Z) are not really characters in the Csense of the word.
您只需键入Ctrl+D(Unix) 或Ctrl+Z(Windows) 即可假装文件已到达结尾。Ctrl+D(或Ctrl+Z) 并不是真正C意义上的字符。
If you run your program with input redirection, the EOF is the actual end of file, not a make belief one./a.out < source.c
如果你使用输入重定向运行你的程序,EOF 是文件的实际结尾,而不是一个让人相信的结尾./a.out < source.c
回答by Petros
getchar() reads a single character of input and returns that character as the value of the function. If there is an error reading the character, or if the end of input is reached, getchar() returns a special value, represented by "EOF".
getchar() 读取输入的单个字符并将该字符作为函数的值返回。如果读取字符时出错,或者到达输入的末尾,getchar() 将返回一个特殊值,由“EOF”表示。
回答by Revanth Kumar
According to the Definition of getchar() it reads a character from the standard input. Unfortunately stdin is mistaken for keyboard which might not be the case for getchar. getchar uses a buffer as stdin and reads a single character at a time. In your case since there is no EOF the getchar and putchar are running multiple times and it looks to you as it the whole string is being printed out at a time. make a small change and you will understand:
根据 getchar() 的定义,它从标准输入读取一个字符。不幸的是,stdin 被误认为是键盘,而 getchar 可能不是这种情况。getchar 使用缓冲区作为标准输入并一次读取一个字符。在您的情况下,由于没有 EOF,因此 getchar 和 putchar 运行多次,并且在您看来,整个字符串一次打印出来。做一个小小的改变,你就会明白:
putchar(c);
printf("\n");
c = getchar();
Now look at the output compared to the original code.
现在查看与原始代码相比的输出。
Another example that will explain you the concept of getchar and buffered stdin :
另一个示例将向您解释 getchar 和缓冲 stdin 的概念:
void main(){
int c;
printf("Enter character");
c = getchar();
putchar();
c = getchar();
putchar();
}
Enter two characters in the first case. The second time when getchar is running are you entering any character? NO but still putchar works.
在第一种情况下输入两个字符。第二次运行 getchar 时是否输入任何字符?不,但仍然 putchar 工作。
This ultimately means there is a buffer and when ever you are typing something and click enter this goes and settles in the buffer. getchar uses this buffer as stdin.
这最终意味着有一个缓冲区,当你输入一些东西并点击输入时,它会在缓冲区中稳定下来。getchar 将此缓冲区用作标准输入。

