getchar()
功 能:從I/O流中讀字符
原 型:int getchar(void);
#include<stdio.h>
#include<conio.h> /*此頭文件有些系統或者編譯器不支持哦*/
int main()
{
char c;
while ((c=getchar())!='\n') /*每個getchar()依次讀入一個字符*/
printf("%c",c); /*按照原樣輸出*/
getch(); /*等待按任一健*/
return 0;
}
輸入:qwerty按下回車鍵
輸出如下:qwerty press any.....
解釋如下:先將這些字符存入緩衝區裏面,遇到回車後getchar()讀,依次輸出qwerty,getchar()讀到回車後就循環終止了.....
getch()
函數名:getch():
頭文件:conio.h
功 能:從控制檯讀取一個字符,但不顯示在屏幕上
原 型:int getch(void)
返回值:讀取的字符
getche
頭文件:conio.h
功 能:從控制檯取字符(帶回顯)
用 法:int getche(void);
(1) getch()和getche()函數
這兩個函數都是從鍵盤上讀入一個字符。其調用格式爲:
getche();
兩者的區別是: getch()函數不將讀入的字符回顯在顯示屏幕上, 而getche()函數卻將讀入的字符回顯到顯示屏幕上(getche()讀入之後不管按不按回車立即回顯)。
利用回顯和不回顯的特點, 這兩個函數經常用於交互輸入的過程中完成暫停等功能。
#include<stdio.h>
#include<conio.h>
int main()
{
int i,k=0;
char tmp[20];
for ( i=0 ; i<20; i++ )
{
tmp[i] = getch(); //關鍵點1 //
tmp[i]=getchar();
//printf ( "%d=[%c]\n",i,tmp [i] );
if ( tmp[i]=='\r' )
{
tmp [i] = '\0' ; //關鍵點2
break ;
}
putchar ( '*' ) ;
}
printf("\n");
while(tmp[k]!='\0')
printf("%c",tmp[k++]);
return 0;
}