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

See Also

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

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.