strtod is the C standard library function for converting an ASCII string into a floating-point number.
strtod
is the C standard library function (found in the ANSI standard, section 2.13.2.4 - found in stdlib.h
) which converts a string into a floating point number.
double strtod(const char *ptr, char **endptr)
- ptr: A pointer to the string that will be converted
endptr: A pointer to where the last successful character match occurred.
For example, calling
strtod
on the string"123abc"
yields an endptr which is equal toptr + 3
. This is often used to test whether or not a string conversion went correctly - if no conversion took place, then*endptr == ptr
.Note that this does not occur if endptr is NULL.
According to ANSI,
The number may consist of an optional sign, a string of digits with an optional decimal character, and an optional e or E followed by a optionally signed exponent.
(This effectively allows both "normal" decimal numbers, as well as scientific notation).
When the converter experiences either an overflow or underflow, errno
is set to ERANGE, and either 0 (for underflows) or HUGE_VAL (for overflows) - it is thus recommended to clear errno
before calling this function, otherwise an underflow error and the conversion from "0" are impossible to differentiate.