getch函数是无回显的从把持台获得一个字符。以操纵getch函数让顺序调试运行结束后等候编程者按下键盘才返回编纂界面,即随意率性键持续后果。下面小编就跟你们详细介绍下c语言中getch的用法,但愿对你们有用。
c语言中getch的用法如下:
[cpp] view plain copy
#include
#include
int main
int i ;
i = getch;
printf"press any key to continue";
printf"%d", i;
return 0;
Windows下getch在conio.h的'头文件中,但conio.h不是规范库文件,C standard library,ISO C 和POSIX规范中均没有界说。当然Linux系统中会没有这个头文件,网上说在curses.h,然后下载一个库,但弄了半天也没成功获得,从网上找到了一个办法实现getch的功效。
[cpp] view plain copy
int getch
struct termios tm, tm_old;
int fd = STDIN_FILENO,c;
if tcgetattrfd, &tm < 0
return -1;
tm_old = tm;
cfmakeraw&tm;
if tcsetattrfd, TCSANOW, &tm < 0
return -1;
c = fgetcstdin;
if tcsetattrfd,TCSANOW,&tm_old < 0
return -1;
return c;
间接可以这样用:
[cpp] view plain copy
/******************************************************************
*描述: 实现随意率性键持续
*参数: void
*返回值: void
*******************************************************************/
void press_key
printf"随意率性键持续...";
getch;
头文件
[cpp] view plain copy
#include
#include
#include
#include