Previous Page
Next Page


Converts a string to a floating-point number

#include <stdlib.h>
double atof ( 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.


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

See Also

strtod( ), atoi( ), atol( ), atoll( ), strtol( ), strtoll( )

Previous Page
Next Page