我做了一个非常简单的程序:
#include <stdio.h>
int main()
{
double x;
printf("Write your number \n");
scanf ("%f", &x);
printf("You've written %f \n", x);
return 0;
}
结果出现了奇怪的数字(无论我给出什么 x):
“你写了 8309626105313258000000000000000000000000000000000000000000
”
这是怎么回事?当我将所有数字更改为“int”类型时,该程序工作正常。
最佳答案
看看在启用警告的情况下编译它时会发生什么:
amb@nimrod-ubuntu:~/so$ gcc -Wall x.c -o x
x.c: In function ‘main’:
x.c:6:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat]
将 %f
更改为 %lf
并且 scanf
和 printf
函数将正确地接受 双
。
关于c - 在C中简单使用双数的printf和scanf的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26577194/