C Programming/C Reference/stdlib.h/strtod

< C Programming < C Reference < stdlib.h

strtod (string to double) converts a character string to a double precision floating-point value[1]. It is defined as:

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

Overview

The strtod function parses the C string str interpreting its content as a floating point number and returns its value as a double. If endptr is not a null pointer, the function also sets the value pointed by endptr to point to the first character after the number.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax resembling that of floating point literals, and interprets them as a numerical value. A pointer to the rest of the string after the last valid character is stored in the object pointed by endptr.

A valid floating point number for strtod is formed by a succession of:

If the first sequence of non-whitespace characters in str does not form a valid floating-point number as just defined, or if no such sequence exists because either str is empty or contains only whitespace characters, no conversion is performed.

Parameters

str

endptr

Return value

On success, the function returns the converted floating point number as a double value. If no valid conversion could be performed, a zero value (0.0) is returned. If the correct value is out of the range of representable values, a positive or negative HUGE_VAL is returned, and the global variable errno is set to ERANGE. If the correct value would cause underflow, zero is returned and errno is set to ERANGE.

The strtod function is included in stdlib.h.

References

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.