2.6.2 字符输入函数getchar
getchar 函数的功能是从键盘上读一个字符作为函数值。其一般调用形式为:
- getchar();
通常把输入的字符赋予一个字符变量,构成赋值语句,如程序2.9中代码。
【程序 2.9】输入输出单个字符:test9.c。
- #include <stdio.h>
- void main()
- {
- char c; /*定义字符变量c*/
- printf("input a character:"); /*打印提示信息*/
- c=getchar(); /*将读取的字符赋给变量c*/
- putchar(c); /*输出字符变量c*/
- }
程序运行结果如下(↙表示回车):
- input a character:b↙
- b