複製鏈接
請複製以下鏈接發送給好友

getch

鎖定
getch是一個計算機函數,在windows平台下從控制枱無回顯地取一個字符,在linux下是有回顯的。用法是int getch(void)。
getch 是 _getch() 的棄用別名,儘管它依然受到支持,微軟仍建議使用遵循C命名規則的 _getch() [1] 
中文名
getch
概    述
在windows平台下從控制
用 法
int getch(void);
返回值
從鍵盤上讀取到的字符

getch函數名

getch

getch用法

int getch(void);
在 Linux 平台下時(即包含的是 curses.h ),還應該在使用函數之前使用 initscr() ,使用完畢之後調用 endwin() 。否則的話不需輸入就會返回。

getch返回值

從鍵盤上讀取到的字符

getch頭文件

#include <conio.h>

getch程序例

Windows 平台
#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>
intmain(void)
{
    char ch;
    init scr();

    printf("Input a char acter:");
    ch=getch();
    printf("\nYou input a '%c'\n",ch);
    endwin();
    return 0;
}
在 Windows/MS-DOS 中,也可以利用 getch() 函數讓程序調試運行結束後等待編程者按下鍵盤才返回編輯界面,用法:包含 conio.h 頭文件後,在主函數結尾,return 0; 之前加上 getch(); 即可
這個函數可以讓用户按下任意鍵而不需要回車就可以接受到用户的輸入。可以用來作為“press any key to continue”的實現。
參考資料