Name
atof
Synopsis
Converts a string to a floating-point number
#include <stdlib.h> doubleatof( const char *s);
The atof() function
converts a string of characters representing a numeral into a
floating-point number of type double. The string must be in a customary
floating-point numeral format, including scientific notation (e.g.,
0.0314 or 3.14e-2). The conversion ignores any
leading whitespace (space, tab, and newline) characters. A minus
sign may be prefixed to the mantissa or exponent to make it
negative; a plus sign in either position is permissible.
Any character in the string that cannot be interpreted as part
of a floating-point numeral has the effect of terminating the input
string, and atof() converts only
the partial string to the left of that character. If the string
cannot be interpreted as a numeral at all, atof() returns 0.
Example
char string[ ] = " -1.02857e+2 \260C"; // symbol for degrees Celsius
double z;
z =atof(string);
printf( "\"%s\" becomes %.2f\n", string, z );This code produces the following output:
" -1.02857e+2 °C" becomes -102.86
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access