我试图使用atof和接收模糊输出将字符数组转换为c中的double。
printf("%lf\n",atof("5"));版画
262144.000000我惊呆了。有人能解释一下我哪里出了问题吗?
发布于 2013-12-26 15:35:34
确保包含了atof和printf的标题。如果没有原型,编译器将假设它们返回int值。当这种情况发生时,结果是未定义的,因为这与double的实际返回类型不匹配。
#include <stdio.h>
#include <stdlib.h>无原型
$ cat test.c
int main(void)
{
printf("%lf\n", atof("5"));
return 0;
}
$ gcc -Wall -o test test.c
test.c: In function ‘main’:
test.c:3:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
test.c:3:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
test.c:3:5: warning: implicit declaration of function ‘atof’ [-Wimplicit-function-declaration]
test.c:3:5: warning: format ‘%lf’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat]
$ ./test
0.000000原型
$ cat test.c
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("%lf\n", atof("5"));
return 0;
}
$ gcc -Wall -o test test.c
$ ./test
5.000000教训:注意编译器的警告。
发布于 2015-06-03 12:41:03
我解决了一个类似的问题,在小数点之后有一个小数点,并且在小数点之后至少有两个零
printf("%lf\n",atof("5.00"));https://stackoverflow.com/questions/20787235
复制相似问题