getchar()函數的作用是從計算機終端(一般為鍵盤)獲取一個無符號字符。getchar()函數只能接收一個字符,其函數值就是從輸入設備獲取到的字符。
該函數聲明在stdio.h頭文件中,使用的時候要包含stdio.h頭文件。
如:
#include<stdio.h>
int getchar(void);
例:
1 #include "stdio.h" 2 main() { 3 char c; 4 int letters=0,space=0,digit=0,others=0; 5 printf("please input some characters\n"); 6 while((c=getchar())!='\n') { 7 if(c>='a'&&c<='z'||c>='A'&&c<='Z') letters++; else if(c==' ') space++; 8 else if(c>='0'&&c<='9') digit++; 9 else others++; 10 } 11 printf("all in all:char=%d space=%d digit=%d others=%d\n",letters, space,digit,others); 12 }