问题
int main()
{
char str[10]="3.5";
printf("%lf",atof(str));
return 0;
}
This is a simple code I am testing at ideone.com. I am getting the output as
-0.371627
回答1:
You have not included stdlib.h. Add proper includes:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[10]="3.5";
printf("%lf",atof(str));
return 0;
}
Without including stdlib.h, atof()
is declare implicitly and the compiler assumes it returns an int.
回答2:
It could be undefined behavior.
来源:https://stackoverflow.com/questions/14918189/what-is-wrong-with-usage-of-atof-function