Go to the first, previous, next, last section, table of contents.


Parsing of Floats

These functions are declared in `stdlib.h'.

Function: double strtod (const char *string, char **tailptr)
The strtod ("string-to-double") function converts the initial part of string to a floating-point number, which is returned as a value of type double.

This function attempts to decompose string as follows:

If the string is empty, contains only whitespace, or does not contain an initial substring that has the expected syntax for a floating-point number, no conversion is performed. In this case, strtod returns a value of zero and the value returned in *tailptr is the value of string.

In a locale other than the standard "C" or "POSIX" locales, this function may recognize additional locale-dependent syntax.

If the string has valid syntax for a floating-point number but the value is outside the range of a double, strtod will signal overflow or underflow as described in section Error Reporting by Mathematical Functions.

strtod recognizes four special input strings. The strings "inf" and "infinity" are converted to @math{@infinity{}}, or to the largest representable value if the floating-point format doesn't support infinities. You can prepend a "+" or "-" to specify the sign. Case is ignored when scanning these strings.

The strings "nan" and "nan(chars...)" are converted to NaN. Again, case is ignored. If chars... are provided, they are used in some unspecified fashion to select a particular representation of NaN (there can be several).

Since zero is a valid result as well as the value returned on error, you should check for errors in the same way as for strtol, by examining errno and tailptr.

Function: float strtof (const char *string, char **tailptr)
Function: long double strtold (const char *string, char **tailptr)
These functions are analogous to strtod, but return float and long double values respectively. They report errors in the same way as strtod. strtof can be substantially faster than strtod, but has less precision; conversely, strtold can be much slower but has more precision (on systems where long double is a separate type).

These functions are GNU extensions.

Function: double atof (const char *string)
This function is similar to the strtod function, except that it need not detect overflow and underflow errors. The atof function is provided mostly for compatibility with existing code; using strtod is more robust.

The GNU C library also provides `_l' versions of these functions, which take an additional argument, the locale to use in conversion. See section Parsing of Integers.


Go to the first, previous, next, last section, table of contents.