EX4.21 getchar();
给出程序的运行结果#include <stdio.h>
int main()
{
char a,b;
int s;
printf("please input a and b:\n");
a=getchar();
b=getchar();
s=a+b;
printf("a=%c,b=%c",a,b);
return 0;
}
程序运行时从键盘输入:
1<空格>2<回车>
C.a=1,b=
please input a and b:
1 2
a=1,b= s=81
Q1:为什么a=1,b=b无输出值
Q2:
查表
0011 0001 49 31 1
0011 0010 50 32 2
为什么s=81 要连续输入。
b=空格 getchar()可以每次接收一个字符,可以接收空格字符,所以空格被保存到了变量b里面。getchar()不会跳过空格读取下一个字符,和scanf()不同。 代码不太好看,建议缩进{:10_256:}
页:
[1]