另外大家平時主要是利用conio.h這個頭文件中的getch()函數,即讀取鍵盤字符但是不顯示出來(without echo),但是含有conio.h的程序在linux無法直接編譯通過,因爲linux沒有這個頭文件,除了利用上述的兼容包外還可以在linux採用原生的方法達到同樣的效果,那就是利用linux系統的命令stty –echo,它代表不顯示輸入內容,源代碼如下。
//in windows
#include<stdio.h>
#include<conio.h>
int main(){
char c;
printf("input a char:");
c=getch();
printf("You have inputed:%c \n",c);
return 0;
}
//in linux
#include<stdio.h>
int main(){
char c;
printf("Input a char:");
system("stty -echo");
c=getchar();
system("stty echo");
printf("You have inputed:%c \n",c);
return 0;
}