C中的getchar()完成而不按Enter键 [英] getchar() in C is completed without pressing Enter

查看:285
本文介绍了C中的getchar()完成而不按Enter键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的上一篇文章中,我发现getchar()只有在按Enter键时才会完成。让我们考虑一下这段代码:

From my previous post, I come to know that getchar() completes only when we press Enter. Let's consider this code:

#include<stdio.h>
main()
{
  getchar();
  getchar();
  getchar();
  getchar();
  getchar();


}

我预计它会像这样运行:我按下某个键1然后按Enter键,然后按键2键,然后按键3键,然后按键3键,然后按键4键,输入键,最后一键5键,输入,程序应立即终止。这不是实际发生的事情。会发生什么:我按下某个键1然后按Enter键,然后按键2按Enter键,然后按键3和回车键,程序最终终止!

I expected it to run like this: I press some key1 then press Enter, then key2 an Enter, then key3 and Enter, then key4 and Enter and at last key5+Enter, the program should terminate now. This is not what actually happens. What happens is this: I press some key1 then press Enter, then key2 an Enter, then key3 and Enter, the program eventually terminates!



  • 为什么最后两个getchar()不起作用?

我观察到的另一个奇怪的事情是,如果我这样做:key1,key2,key3,key4 + Enter然后程序终止。例如。如果我连续按q,w,e和r然后按Enter键,程序就会终止。

Another weird thing that I observed is that if I do: key1,key2,key3,key4+Enter then the program terminates. E.g. If I press q,w,e and r in succession and then Enter, the program terminates.



  • 为什么不是所有的getchar()都要求输入?这是否意味着getchar()将任何其他键作为Enter?但是下一个键是否被作为下一个getchar()的输入?

让我们考虑另一个代码:

Let's consider another code:

#include<stdio.h>
main()
{

  int c=getchar();
  int d=getchar();
  int e=getchar();
  printf("2 getchar are remaining\n");
  int f=getchar();
  int g=getchar();
  printf(" c is %d, d is %d, e is %d, f is %d and g is %d",c,d,e,f,g);

} 

我输入:ABCDEFG然后回车。当我按下C或D时,应该打印 2 getchar剩余行。但它最后打印,意味着所有getchar()s同时执行 - 这很奇怪。

I input: ABCDEFG then Enter. The line 2 getchar are remaining should be printed as soon as I press C or D. But it is printed at last, means that all the getchar()s get executed simultaneously-- this is weird.



  • 程序是否逐行执行?即在第三个getchar之后,printf()应该可以工作。但是当所有的getchar()被执行时它终于工作了。


推荐答案

按Enter键后,getchar()完成并不正确。只要有要读取的字符,getchar()就会完成。这种差异非常重要:例如,如果您使用带有重定向到文件的标准输入的程序,请参阅:

It's not true that getchar() completes after you press enter. getchar() completes whenever there are characters to read. This difference is significant: see, for example, if you use your program with the standard input redirected to a file:

$ hexdump -C abcd_file 
00000000  61 62 63 64 65                                    |abcde|
00000005

$ ./in < abcd_file 
$

请注意,abcd_file是一个包含abcde的文件,没有换行和您的程序完成后无需任何换行。那是因为文件一直在提供字符,而不是等待换行符。

Notice that "abcd_file" is a file that contains "abcde", no newline and your program finishes without requiring a newline anywhere. That's because the file is providing characters all the time and not waiting for a newline.

另一方面,公共终端或终端仿真器有一个被调用的操作模式规范模式。规范模式意味着终端支持命令行处理设施,并且在用户按下ENTER之前不会发出可用字符的信号。这就是不正确的getchar()等待ENTER故事的来源。您可以将终端切换到规范模式,并查看它检索所有字符而无需按Enter键:

Common terminals or terminal emulators, on the other hand, have an operation mode that is called the "canonical mode". Canonical mode means that the terminal supports "command line processing facilities" and will not signal an available character until the user presses ENTER. That's where the incorrect "getchar() waits for ENTER" story comes from. You can switch your terminal out of canonical mode and see it retrieving all the characters without the need for pressing enter:

$ stty -icanon; ./in; stty icanon
ggggg$

在这种情况下,没有输入的5个字符使程序完成。

In this case 5 characters without an enter made the program to finish.

最后,getchar()看起来像早期返回的原因是因为它也返回了ENTER字符。所以a\\\
b\\\
c\\\
是6个字符,前5个是由getchar()返回的,第6个是在程序完成后从终端队列中删除的。键入abcd \ n也意味着getchar()将立即可用于连续5次读取,因为终端队列中存储了5个字符。

Finally, the reason for getchar() look like it is returning early is because it also returns the ENTER characters. So "a\nb\nc\n" is 6 characters, the first 5 are returned by getchar(), the sixth is deleted from the terminal queue after the program finishes. Typing "abcd\n" also means that getchar() will be immediatelly available for 5 consecutive reads, because there are 5 characters stored in the terminal queue.

http://www.gnu.org/software/libc /manual/html_node/Noncanonical-Input.html#Noncanonical-Input

这篇关于C中的getchar()完成而不按Enter键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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