C多个字符变量赋值

来源:百度知道 编辑:UC知道 时间:2024/06/14 15:15:03
main()
{
char v1,v2,v3;
printf("Please input the kind of v1\n");
scanf("%c",&v1);

printf("Please input the kind of v2\n");
scanf("%c",&v2);

printf("Please input the kind of v3\n");
scanf("%c",&v3);
printf("%c,%c,%c",v1,v2,v3);
}
为什么v2赋不上值呢?

不是赋不上值,是你按换行的时候赋了个换行符。。。
#include <stdio.h>

int main()
{
char v1,v2,v3;
printf("Please input the kind of v1\n");
scanf("%c",&v1);
getchar();
printf("Please input the kind of v2\n");
scanf("%c",&v2);
getchar();
printf("Please input the kind of v3\n");
scanf("%c",&v3);
printf("%c,%c,%c",v1,v2,v3);
return 0;
}

你输入的时候输入的回车\r\n,其中\r被用来判断输入结束,\n还存在缓冲区里面,要求输入v2的时候就把回车\n赋给v2了

可以用gerchar();把这个字符吃掉,也可以用fflush()清除缓冲区