The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group


strtod - convert string to a double-precision number


#include <stdlib.h>

double strtod(const char *str, char **endptr);


The strtod() function converts the initial portion of the string pointed to by str to type double representation. First it decomposes the input string into three parts: an initial, possibly empty, sequence of white-space characters (as specified by isspace()); a subject sequence interpreted as a floating-point constant; and a final string of one or more unrecognised characters, including the terminating null byte of the input string. Then it attempts to convert the subject sequence to a floating-point number, and returns the result.

The expected form of the subject sequence is an optional + or - sign, then a non-empty sequence of digits optionally containing a radix character, then an optional exponent part. An exponent part consists of e or E, followed by an optional sign, followed by one or more decimal digits. The subject sequence is defined as the longest initial subsequence of the input string, starting with the first non-white-space character, that is of the expected form. The subject sequence is empty if the input string is empty or consists entirely of white-space characters, or if the first character that is not white space is other than a sign, a digit or a radix character.

If the subject sequence has the expected form, the sequence starting with the first digit or the radix character (whichever occurs first) is interpreted as a floating constant of the C language, except that the radix character is used in place of a period, and that if neither an exponent part nor a radix character appears, a radix character is assumed to follow the last digit in the string. If the subject sequence begins with a minus sign, the value resulting from the conversion is negated. A pointer to the final string is stored in the object pointed to by endptr, provided that endptr is not a null pointer.

The radix character is defined in the program's locale (category LC_NUMERIC). In the POSIX locale, or in a locale where the radix character is not defined, the radix character defaults to a period (.).

In other than the POSIX locale, other implementation-dependent subject sequence forms may be accepted.

If the subject sequence is empty or does not have the expected form, no conversion is performed; the value of str is stored in the object pointed to by endptr, provided that endptr is not a null pointer.

The strtod() function will not change the setting of errno if successful.

Because 0 is returned on error and is also a valid return on success, an application wishing to check for error situations should set errno to 0, then call strtod(), then check errno.


Upon successful completion, strtod() returns the converted value. If no conversion could be performed, 0 is returned,  and errno may be set to [EINVAL].

If the correct value is outside the range of representable values, ±HUGE_VAL is returned (according to the sign of the value), and errno is set to [ERANGE].

If the correct value would cause an underflow, 0 is returned and errno is set to [ERANGE].


The strtod() function will fail if:
The value to be returned would cause overflow or underflow.

The strtod() function may fail if:

No conversion could be performed.








isspace(), localeconv(), scanf(), setlocale(), strtol(), <stdlib.h>, the XBD specification, Locale .


Derived from Issue 1 of the SVID.

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]