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
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
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.
%lf
is only proper in C99's printf()
and not in C89's. –
Paniagua It could be undefined behavior.
%lf
is the correct specifier for double, there's no issue with that. It's just that "If the converted value falls out of range of the return type, the return value is undefined" which is the same reason why atoi()
shouldn't be used –
Actinism © 2022 - 2024 — McMap. All rights reserved.
%lf
is perfectly fine for printing adouble
. – Overlive