getchar函数()在C给输出 [英] getchar() is giving output in C

查看:170
本文介绍了getchar函数()在C给输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该这样做的节目,

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

我希望它显示黑屏,直到我按下键盘上的任何字符。但它确实是很奇怪的。它显示不管我preSS。它永远不会终止,直到我preSS Enter键。

I expect it to show a blank screen until I hit any character on the keyboard. But what it does is quite weird. It displays whatever I press. It never terminates until I press Enter.

据我所知,getchar函数()应该只是读取一个字符。它不应该输出什么。

As far as I know, getchar() should just read one character. It should not output anything.


      
  • 为什么打印的每一个字符,我输入?

  •   

编辑:

为什么不的getchar()停止读取一个字符后,例如在这code:

Why doesn't getchar() stop after reading one character, e.g. in this code:

   #include <stdio.h>  

   main()  

   {  

    getchar();  

   printf("Done");  


  }  

程序应该读一个字符后打印完成。

The program should print Done after reading one character.

推荐答案

您的程序将不会终止,直到的getchar()完成。 的getchar()不完整的,直到输入缓冲区填充。输入缓冲器不填充直到preSSEnter键。

Your program won't terminate until getchar() completes. getchar() does not complete until the input buffer is populated. The input buffer is not populated until you press 'Enter'.

您所看到的字符是你输入的字符。这是默认的终端驱动的行为,而不是由程序驱动的。

The character you are seeing is the character you are typing. This is default terminal-driven behavior, not driven by your program.

这篇关于getchar函数()在C给输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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