x和y的输出值一直是0,如何解决?
#include<stdio.h>
int main(void)
{
long a,b;
float x,y;
scanf("%d,%d\n",&a,&b);
scanf("%5.2f,%5.2f\n",&x,&y);
printf("a=%d,b=%d\n",a,b);
printf("x=%d,y=%d\n",x,y);
return 0;
}
#include<stdio.h>
int main(void)
{
long a,b;
float x,y;
scanf("%d,%d\n",&a,&b);
scanf("%5.2f,%5.2f\n",&x,&y);
printf("a=%d,b=%d\n",a,b);
printf("x=%d,y=%d\n",x,y);
return 0;
}
scanf不要用精度去控制输入,这虽然不报错。但明显是有问题的,不要把字符串控制长度的方式用在数字输入方面控制,是有差别的。
#include <stdio.h>
int main(void)
{
long a, b;
float x, y;
scanf("%ld%ld", &a, &b);
scanf("%f%f", &x, &y);
printf("a=%ld,b=%ld\n", a, b);
printf("x=%.2f,y=%.2f\n", x, y);
return 0;
}
也可以这样只输入宽度,但是scanf输入不要用小数点去控制精度
#include <stdio.h>
int main(void)
{
long a, b;
float x, y;
scanf("%ld%ld\n", &a, &b);
scanf("%15f%15f", &x, &y);
printf("a=%ld,b=%ld\n", a, b);
printf("x=%.2f,y=%.2f\n", x, y);
return 0;
}