为什么用我的教授2的getchar();? [英] Why is my professor using two getchar();?
问题描述
为什么用我的教授2的getchar();在我们的C程序教程结束了吗?
Why is my prof using two getchar(); at the end of our C program tutorials?
和什么是这个更好的方式?
And what is the "better way" for this?
推荐答案
最好的更好的方法是不添加任何code,要尽力保持控制台窗口打开:直接从控制台窗口启动程序
The best better way is not to add any code to try and keep the console window open: start your program right from the console window.
如果您必须启动从IDE程序,并希望程序不会终止用户presses进入之前,一个单一的的getchar()
应该这样做。
If you must start your program from the IDE and want the program to not terminate before the user presses Enter, one single getchar()
should do it.
第二个最好的更好的方式来使程序终止用户presses一个键后,是要始终确保有没有输入挂起,并使用一个单一的的getchar()
。
The second best better way to make the program terminate after the user presses a key, is to always make sure there are no inputs pending and use one single getchar()
.
你的老师用2 的getchar原因()
,我想,是已经有从previous输入的输入缓冲区字符。从输入消耗所有的字符,直到并包括回车,这是通常的:
The reason your teacher uses 2 getchar()
, I guess, is that there is already a character in the input buffer from previous inputs. To consume all characters from the inputs, up to and including the ENTER, this is usual:
int ch;
/* ... */
printf("Press Enter"); fflush(stdout);
while (((ch = getchar()) != '\n') && (ch != EOF)) /* void */;
这篇关于为什么用我的教授2的getchar();?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!