getch

来源:互联网 发布:重庆生活频道 网络直播 编辑:程序博客网 时间:2024/05/01 14:27

转自百度百科:http://baike.baidu.com/view/751663.htm

getch 

编辑本段函数名

getch

编辑本段功 能

在windows平台下从控制台无回显地取一个字符,在linux下是有回显的。

编辑本段用 法

int getch(void);
在linux平台下时(即包含的是curses.h),还应该在使用函数之前使用initscr(),使用完毕之后调用endwin().否则的话不需输入就会返回。

编辑本段返回值

从键盘上读取到的字符

编辑本段头文件

#include <conio.h>

编辑本段程序例

window 平台

#include <stdio.h>
#include <conio.h>
int main(void)
{
char ch;
printf("Input a character:");
ch = getch();
printf("\nYou input a '%c'\n", ch);
return 0;
}
注:Windows下不推荐使用POSIX。建议使用使用标准C++相似的名称:_getch。详情请参阅c++标准文件

linux 平台

#include <stdio.h>
#include <curses.h>
int main(void)
{
char ch;
initscr();
printw("Input a character:");[1]
ch = getch();
printw("\nYou input a '%c'\n", ch);[1]
endwin();
return 0;
}
在WINDOWS/MS-DOS中,也可以利用getch()函数让程序调试运行结束后等待编程者按下键盘才返回编辑界面,用法:包含conio.h头文件后,在主函数结尾,return 0;之前加上getch();即可
这个函数可以让用户按下任意键而不需要回车就可以接受到用户的输入。可以用来作为“press any key to continue”的实现。
参考资料
  • 1.  Manpage for printw .Manpages[引用日期2012-12-29]

 

原创粉丝点击