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 not representable because of overflow, strtod returns either positive or negative HUGE_VAL (see section Mathematics), depending on the sign of the value. Similarly, if the value is not representable because of underflow, strtod returns zero. It also sets errno to ERANGE if there was overflow or underflow.

Since the value zero which is returned in the error case is also a valid result the user should set the global variable errno to zero before calling this function. So one can test for failures after the call since all failures set errno to a non-zero value.

Function: float strtof (const char *string, char **tailptr)
This function is similar to the strtod function but it returns a float value instead of a double value. If the precision of a float value is sufficient this function should be used since it is much faster than strtod on some architectures. The reasons are obvious: IEEE 754 defines float to have a mantissa of 23 bits while double has 53 bits and every additional bit of precision can require additional computation.

If the string has valid syntax for a floating-point number but the value is not representable because of overflow, strtof returns either positive or negative HUGE_VALf (see section Mathematics), depending on the sign of the value.

This function is a GNU extension.

Function: long double strtold (const char *string, char **tailptr)
This function is similar to the strtod function but it returns a long double value instead of a double value. It should be used when high precision is needed. On systems which define a long double type (i.e., on which it is not the same as double) running this function might take significantly more time since more bits of precision are required.

If the string has valid syntax for a floating-point number but the value is not representable because of overflow, strtold returns either positive or negative HUGE_VALl (see section Mathematics), depending on the sign of the value.

This function is a GNU extension.

As for the integer parsing functions there are additional functions which will handle numbers represented using the grouping scheme of the current locale (see section Parsing of Integers).

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.


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