头文件:#include <stdio.h>
getchar();
函数getchar();用于从标准控制台读取字符。
int getchar(void);
//注意:该函数没有参数
返回值问题:
1.函数的返回值为用户输入的第一个字符的ASCII码,若出错则返回-1;
2.如果用户在回车之前不止输入了一个字符,其他字符会保留在键盘缓冲区,等待后续getchar()调用读取。//键盘缓冲区的知识需要补充,这个问题没有解决。
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
int c=97;
int a;
// a=getchar();//推荐尽量这样去写
while(c==getchar())//预想是,输入c之后还是紧接着运行,但输入c之后就停止了//仿佛getchar有跳出的功能。
// c=getchar();
{
printf("%d",c);
}
return 0;
}
说明:
getchar();非真正的函数,而是getc(stdin)定义。//
这个小例子也比较有意思
#include<stdio.h>
void main( ){
int c;
while( (c=getchar()) != '\n' ) //从控制台流中读取字符,直到按回车键结束
printf ("%c", c); //输出读取内容
}
#include<stdio.h> void main() { char ch = getchar(); char ch2 = getchar(); printf("You put char is %c\n",ch); printf("You put char is %c\n",ch2); return; }