一条题目求解
已有如下定义和输入语句,若要求a1,a2,c1,c2的值分别为10,20,A和B,当从第一列开始输入数时,正确的数据输入方式是 。
int a1,a2; char c1,c2;
scanf(“%d%c%d%c”,&a1,&c1,&a2,&c2);
A)10A 20B <CR> B)10 A 20 B <CR>
C)10(Tab)A(Tab)20(Tab)B <CR>
D)10A20 B <CR>
A?还是B?
why?
[解决办法]
%c:characters; char*. The next input characters are placed in the indicated array, up to the number given by the width field; the default is 1. No '\0 ' is added. The normal skip over white space characters is suppressed in this case; to read the next non-white space character, use %1s.
from The_C_programming_Language,Appendix B: Standard Library 1.3
[解决办法]
A,因为scanf()函数中,d%是从第一个是数字的数据开始读取,c%是从开始读取到第一个空格结束。