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
The atof() function converts a character string to a double-precision floating-point value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type.
The atof() function ignores all the leading whitespace characters until the primary non-whitespace character is found. Then, beginning from this character, it takes as many characters as possible that forms a valid floating-point representation and converts them to a floating point value.
Defined in header <stdlib.h> double atof( const char* str ); Interprets a floating-point value in a byte string pointed to by str . Function discards any whitespace characters (as determined by std::isspace()) until first non-whitespace character is found.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With