getch() 和 getchar() 有什么区别?
getch
和 getchar
函数之间的确切区别是什么?
What is the exact difference between the getch
and getchar
functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
getch
和 getchar
函数之间的确切区别是什么?
What is the exact difference between the getch
and getchar
functions?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
getchar()
是一个从stdin
获取字符的标准函数。getch()
是非标准的。它从键盘获取一个字符(可能与标准输入不同)并且不回显它。getch()
定义在conio.h
中,而 getchar 定义在stdio.h
中getchar()
is a standard function that gets a character from thestdin
.getch()
is non-standard. It gets a character from the keyboard (which may be different from stdin) and does not echo it.getch()
is defined inconio.h
, while getchar is instdio.h
标准 C 函数是
getchar()
,在
中声明。它基本上自古以来就存在。它从标准输入 (stdin
) 读取一个字符,该输入通常是用户的键盘,除非它已被重定向(例如通过 shell 输入重定向字符<
,或者一根管子)。getch()
和getche()
是旧的 MS-DOS 函数,在
中声明,并且在 Windows 系统上仍然很流行。它们不是标准 C 函数;它们并不存在于所有系统上。getch
立即从键盘读取一次击键,无需等待用户按下 Return 键,也不会回显该击键。getche
是相同的,只是它确实 回显。据我所知,getch
和getche
总是从键盘读取;它们不受输入重定向的影响。那么问题自然而然地出现了,如果
getchar
是标准函数,那么如何使用它来读取一个字符而不等待Return键,或者不回显呢?这些问题的答案至少有点复杂。 (事实上,它们足够复杂,我怀疑它们解释了getch
和getche
的持久流行,如果没有其他的话,它们非常容易使用。)问题是
getchar
无法控制回显和输入缓冲等细节——就 C 而言,这些都是较低级别的、与系统相关的问题。但了解
getchar
假定的基本输入模型很有用。令人困惑的是,通常有两种不同级别的缓冲。当用户在键盘上键入按键时,操作系统的终端驱动程序会读取这些按键。通常,在默认模式下,终端驱动程序会在键入时立即回显击键(以便用户可以看到他们正在键入的内容)。通常,在默认模式下,终端驱动程序还支持一定量的行编辑 - 例如,用户可以按删除或退格键来删除意外键入的字符。为了支持行编辑,终端驱动程序通常会在输入缓冲区中收集字符。只有当用户按下 Return 键时,该缓冲区的内容才可供调用程序使用。 (仅当标准输入实际上是键盘或其他串行设备时,才会出现此级别的缓冲。如果标准输入已重定向到文件或管道,则终端驱动程序不起作用,并且此级别的缓冲不适用。)
stdio 包将字符从操作系统读取到自己的输入缓冲区中。
getchar
只是从该缓冲区中获取下一个字符。当缓冲区为空时,stdio 包尝试通过从操作系统读取更多字符来重新填充缓冲区。因此,如果我们跟踪程序第一次调用 getchar 时发生的情况:stdio 发现其输入缓冲区为空,因此它尝试从操作系统读取一些字符,但没有还没有任何可用字符,因此
read
调用会阻塞。同时,用户可能正在键入一些字符,这些字符正在终端驱动程序的输入缓冲区中累积,但用户尚未按回车键。最后,用户按下 Return 键,被阻止的 read 调用返回,将整行的字符返回到 stdio,stdio 使用它们来填充其输入缓冲区,其中然后,它将第一个返回到对getchar
的初始调用,该调用一直在耐心等待。 (然后,如果程序第二次或第三次调用getchar
,可能还有更多字符——用户键入的行上的下一个字符——在 stdio 中可用getchar
立即返回的输入缓冲区 有关更多信息,请参阅 部分6.2 其中 C 课程笔记。)正如您所看到的,getchar 和 stdio 包无法控制回显或输入行编辑等细节,因为这些细节是在步骤 1 中在终端驱动程序中较早、较低级别处理的。
所以,至少在类Unix操作下系统中,如果您想在不等待 Return 键的情况下读取字符,或者控制是否回显字符,可以通过调整终端驱动程序的行为来实现。细节有所不同,但有一种方法可以打开和关闭回声,以及一种方法(实际上有几种方法)可以打开和关闭输入行编辑。 (至少要了解其中一些详细信息,请参阅这个SO问题,或旧C 常见问题解答列表中的问题 19.1< /a>.)
输入时当行编辑关闭时,操作系统可以立即返回字符(无需等待回车键),因为在这种情况下它不必担心用户可能输入了错误的击键而需要“收回”使用删除或退格键。 (但同样的道理,当一个程序在终端驱动程序中关闭输入行编辑时,如果它想让用户纠正错误,它必须实现自己的编辑,因为它会看到——也就是说,连续的对
getchar
的调用将返回 - 用户的错误字符和删除或退格键的字符代码。)The Standard C function is is
getchar()
, declared in<stdio.h>
. It has existed basically since the dawn of time. It reads one character from standard input (stdin
), which is typically the user's keyboard, unless it has been redirected (for example via the shell input redirection character<
, or a pipe).getch()
andgetche()
are old MS-DOS functions, declared in<conio.h>
, and still popular on Windows systems. They are not Standard C functions; they do not exist on all systems.getch
reads one keystroke from the keyboard immediately, without waiting for the user to hit the Return key, and without echoing the keystroke.getche
is the same, except that it does echo. As far as I know,getch
andgetche
always read from the keyboard; they are not affected by input redirection.The question naturally arises, if
getchar
is the standard function, how do you use it to read one character without waiting for the Return key, or without echoing? And the answers to those questions are at least a little bit complicated. (In fact, they're complicated enough that I suspect they explain the enduring popularity ofgetch
andgetche
, which if nothing else are very easy to use.)And the answer is that
getchar
has no control over details like echoing and input buffering -- as far as C is concerned, those are lower-level, system-dependent issues.But it is useful to understand the basic input model which
getchar
assumes. Confusingly, there are typically two different levels of buffering.As the user types keys on the keyboard, they are read by the operating system's terminal driver. Typically, in its default mode, the terminal driver echoes keystrokes immediately as they are typed (so the user can see what they are typing). Typically, in its default mode, the terminal driver also supports some amount of line editing -- for example, the user can hit the Delete or Backspace key to delete an accidentally-typed character. In order to support line editing, the terminal driver is typically collecting characters in an input buffer. Only when the user hits Return are the contents of that buffer made available to the calling program. (This level of buffering is present only if standard input is in fact a keyboard or other serial device. If standard input has been redirected to a file or pipe, the terminal driver is not in effect and this level of buffering does not apply.)
The stdio package reads characters from the operating system into its own input buffer.
getchar
simply fetches the next character from that buffer. When the buffer is empty, the stdio package attempts to refill it by reading more characters from the operating system.So, if we trace what happens starting when a program calls
getchar
for the first time: stdio discovers that its input buffer is empty, so it tries to read some characters from the operating system, but there aren't any characters available yet, so theread
call blocks. Meanwhile, the user may be typing some characters, which are accumulating in the terminal driver's input buffer, but the user hasn't hit Return yet. Finally, the user hits Return, and the blockedread
call returns, returning a whole line's worth of characters tostdio
, which uses them to fill its input buffer, out of which it then returns the first one to that initial call togetchar
, which has been patiently waiting all this time. (And then if the program callsgetchar
a second or third time, there probably are some more characters -- the next characters on the line the user typed -- available in stdio's input buffer forgetchar
to return immediately. For a bit more on this, see section 6.2 of these C course notes.)But in all of this, as you can see,
getchar
and the stdio package have no control over details like echoing or input line editing, because those are handled earlier, at a lower level, in the terminal driver, in step 1.So, at least under Unix-like operating systems, if you want to read a character without waiting for the Return key, or control whether characters are echoed or not, you do that by adjusting the behavior of the terminal driver. The details vary, but there's a way to turn echo on and off, and a way (actually a couple of ways) to turn input line editing on and off. (For at least some of those details, see this SO question, or question 19.1 in the old C FAQ list.)
When input line editing is turned off, the operating system can return characters immediately (without waiting for the Return key), because in that case it doesn't have to worry that the user might have typed a wrong keystroke that needs to be "taken back" with the Delete or Backspace key. (But by the same token, when a program turns off input line editing in the terminal driver, if it wants to let the user correct mistakes, it must implement its own editing, because it is going to see --- that is, successive calls to
getchar
are going to return -- both the user's wrong character(s) and the character code for the Delete or Backspace key.)getchar
是标准 C,可在 stdio.h 中找到。它从stdin
(标准输入流 = 大多数系统上的控制台输入)读取一个字符。这是一个阻塞调用,因为它要求用户键入一个字符然后按 Enter 键。它将用户输入回显到屏幕。getc(stdin)
100% 等同于getchar
,只不过它也可用于其他输入流。getch
是非标准的,通常出现在旧的过时的 MS DOS 标头 conio.h 中。它的工作方式与 getchar 类似,只是它在第一次击键后不会阻塞,它允许程序继续运行,而无需用户按 Enter 键。它不会将输入回显到屏幕。getche
与getch
相同,也是非标准的,但它会将输入回显到屏幕。getchar
is standard C, found in stdio.h. It reads one character fromstdin
(the standard input stream = console input on most systems). It is a blocking call, since it requires the user to type a character then press enter. It echoes user input to the screen.getc(stdin)
is 100% equivalent togetchar
, except it can also be use for other input streams.getch
is non-standard, typically found in the old obsolete MS DOS header conio.h. It works just likegetchar
except it isn't blocking after the first keystroke, it allows the program to continue without the user pressing enter. It does not echo input to the screen.getche
is the same asgetch
, also non-standard, but it echoes input to the screen.getch() 它只是获取输入,但永远不会将其作为输出显示在屏幕上,尽管我们按下了 Enter 键。
getchar()
当我们按下回车键时,它会获取一个输入并将其显示在屏幕上。getch()
it just gets an input but never display that as an output on the screen despite of us pressing an enter key.getchar()
it gets an input and display it on the screen when we press the enter key.