“getchar()”函数如何能够接受多个字符作为输入?

2023-11-24

这是一个基本的 C 字符计数程序:

#include <stdio.h>
#include <stdlib.h>

int main(){
    long nc = 0;

    while(getchar() != EOF)
        nc++;
    printf("%ld\n" , nc);
}

当我输入“abcde”作为输入时,它显示值 6(触发 EOF 测试后),+1 表示换行符。但我的疑问是getchar()顾名思义,只考虑 1 个字符。但是当我一次性输入“abcde”时,它仍然有效。为什么会这样呢?我在这里遇到了什么问题?


Standard input, by default, is line-buffered with an interactive device. This means that your program won't see any input at all until a complete line is ready, in your case when you hit Enter. One good reason for this is that if the user types her 8 character password, then hits backspace 8 times, and then types her username and hits Enter, then your program only gets her username, and never sees the correction, which is usually what you want when your shell gives the opportunity to edit your input before you send it anywhere.

所以发生的事情本质上是这样的:

  1. 你打电话getchar()。没有可用的输入,因此等待。

  2. You press a. It's not the end of a line, so no input is sent to your program, getchar() has nothing to read, so it still waits.

  3. You press b. It's not the end of a line, so no input is sent to your program, getchar() has nothing to read, so it still waits.

  4. You press c. It's not the end of a line, so no input is sent to your program, getchar() has nothing to read, so it still waits.

  5. You press d. It's not the end of a line, so no input is sent to your program, getchar() has nothing to read, so it still waits.

  6. You press e. It's not the end of a line, so no input is sent to your program, getchar() has nothing to read, so it still waits.

  7. You press Enter. Now it is the end of a line, so the input "abcde\n" is sent to your program.

  8. getchar()现在有输入要读取,所以它返回'a', 增量nc,然后循环返回等待输入。

  9. 立即地,getchar()有更多输入可以从该行的其余字符中读取,因此它返回'b', 增量nc,然后循环返回等待输入。

  10. 立即地,getchar()有更多输入可以从该行的其余字符中读取,因此它返回'c', 增量nc,然后循环返回等待输入。

  11. 立即地,getchar()有更多输入可以从该行的其余字符中读取,因此它返回'd', 增量nc,然后循环返回等待输入。

  12. 立即地,getchar()有更多输入可以从该行的其余字符中读取,因此它返回'e', 增量nc,然后循环返回等待输入。

  13. 立即地,getchar()有更多输入可以从该行的其余字符中读取,因此它返回'\n', 增量nc,然后循环返回等待输入。

  14. If you signified end-of-input, perhaps by pressing Control-D, then getchar() has nothing to read and knows there will never be anything to read, so it returns EOF and your loop ends. If it were not end-of-input, then getchar() would just again wait here for you to enter a new line of input.

So what actually happened here is that getchar() did nothing until you hit Enter. Then, probably before you even took your finger off the Enter key, it ran six times and consumed the six characters of input that you typed. But despite getchar() running six times, you were only prompted to enter something once (twice, if you include having to type Control-D), because getchar() will only wait for your input when it doesn't already have input available and waiting.

在独立终端很常见的时代,实际的终端设备甚至可能直到一行结束才向计算机传输任何字符,并且可能具有少量的板载内存来允许这种本地行-基于编辑,因此计算机本身可能实际上永远不会看到它,直到行尾。在许多人使用的现代 PC 上,操作系统(在终端驱动程序级别下)更可能会缓冲这些字符本身,并仅将它们呈现出来并一次一行地提供给您的程序(除非您特别指定)当然,立即告诉它你想要角色)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

“getchar()”函数如何能够接受多个字符作为输入? 的相关文章

随机推荐