The signature is (give or take restrict
keywords):
double strtod(const char *nptr, char **endptr);
If you pass a non-null pointer as the second argument, it will be returned with the value of nptr
if it could perform no conversion. If it found a genuine zero in the input string, then the value stored in *endptr
won't be nptr
.
char *end;
const char *data = "0.00000";
errno = 0;
double d = strtod(data, &end);
if (end != data)
...a conversion was performed...
else
...trouble...
You can also look at errno
, but you need to zero it before the call because no function in the standard C library or the POSIX library sets errno
to zero.
The standard says:
If the subject sequence is empty or does not have the expected form, no conversion is performed; the value of nptr
is stored in the object pointed to by endptr
, provided that endptr
is not a null pointer.
Returns
The functions return the converted value, if any. If no conversion could be performed, zero is returned. If the correct value overflows and default rounding is in effect (7.12.1), plus or minus HUGE_VAL, HUGE_VALF, or HUGE_VALL is returned (according to the return type and sign of the value), and the value of the macro ERANGE is stored in errno
. If the result underflows (7.12.1), the functions return a value whose magnitude is
no greater than the smallest normalized positive number in the return type; whether errno
acquires the value ERANGE is implementation-defined.