看看这个简单的c程序出了什么问题?

来源:百度知道 编辑:UC知道 时间:2024/07/02 10:24:48
#include<stdio.h>
main()
{
int a,b,c;
char d;
printf("Please input three number:\n");
scanf("%d,%d,%d",&a,&b,&c);
printf("a=%d b=%d c=%d\n",a,b,c);
scanf("%c",&d);
printf("%c",d);
}
为何最后两行不会被执行?gcc编译执行结果如下:
Please input three number:
3,4,5
a=3 b=4 c=5

hot@hot-desktop:~/a$
printf和scanf位置不能调换。

不是吧,可能是把回车\n读到了,你别用字符,用个数字试试

#include<stdio.h>
main()
{
int a,b,c;
char d;
printf("Please input three number:\n");
scanf("%d,%d,%d",&a,&b,&c);
scanf("%c",&d);
printf("a=%d b=%d c=%d\n",a,b,c);
printf("%c",d);
}
输入的时候
Please input three number:
3,4,5c
a=3 b=4 c=5
c
这样就可以出来,应该是那个scanf与printf函数的问题吧

#include<stdio.h>
main()
{
int a,b,c;
char d;
printf("Please input three number:\n");
scanf("%d,%d,%d",&a,&b,&c);
printf("a=%d b=%d c=%d\n",a,b,c);
getchar();
scanf("%c",&d);
printf("%c",d);
}

因为键盘缓冲区,没有刷新,在输入a,b,c的时候的回车给了d
因此下面会多输出了一行;可以fflush(stdin)用它清除缓冲区
#include<stdio.h>
void main()
{
int a,b,c;
char d;
printf("Please input three number:\n&q