我尝试在 C 中使用 scanf() 读入 2 个值,但系统写入内存的值不等于我输入的值。这是代码:
double a,b;
printf("--------\n"); //seperate lines
scanf("%ld",&a);
printf("--------\n");
scanf("%ld",&b);
printf("%d %d",a,b);
如果我输入 1 和 2,CMD 返回正确的,但 b = -858993460 这是我已经尝试过的: 使用 float 或 int 而不是 double,使用 scanf_s,使用 scanf("%d or %f for %i or %li or %lf or %e or %g ),使用 fflush(stdin) 清除键盘缓冲区,读入 b首先,尝试所有可能的组合。我发现 double 在 32 位操作系统上的长度存在问题,因此您被迫使用 scanf("%lf", &f) 读取 double。无论我所做的,第二个值总是错误的。
我在 Windows 7 32 位操作系统的桌面上使用 MS VS express 2012。
最佳答案
使用%lf
格式说明符读取 double 值:
double a;
scanf("%lf",&a);
维基百科有一个 decent reference用于可用的格式说明符。
您还需要使用 %lf
格式说明符来打印结果:
printf("%lf %lf",a,b);
关于c - 在 c 中使用 scanf 读取 double 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13730188/