questions about c



getchar() and putchar()

代码来源<The C programming language(second edition)>

#include <stdio.h>

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

gcc编译,运行后发现,输入字符串按下回车后,输出整个字符串。

书中定义:

  • getchar reads the next input character from a text stream and returns that as its value.
  • The function putchar prints a character each time it is called

getchar()/putchar()每次只读取/输出一个字符。所以为什么结果不是输入一个字符、显示一个字符,而是在输入字符串按下回车后输出整个字符串?中间有缓冲?

stackoverflow上有人问这个问题1。得票最高的答案:

getchar 利用缓冲输入(buffer input)工作,要让getchar读取字符之前需要按下回车。

另外一个重复问题2。提问者想要使用fflush(stdin)清空缓冲区。但是根据ANSI C标准,fflush(stdin)会引发未定义行为。


 Toc
 Tags