CKTIS / C
strtod — convert a string prefix to double
strtod converts a string prefix to double. endptr lets you detect missing conversions and inspect the remaining text.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <stdlib.h>
double strtod(const char *restrict nptr, char **restrict endptr);strtod skips leading whitespace and converts a prefix of a null-terminated string. The decimal-point character depends on the locale.
With non-null endptr, *endptr points to the remaining text. end == text means no conversion; zero alone does not indicate failure. Full consumption also requires *end == '\0'.
Clear errno before checking range errors. Overflow reporting through ERANGE depends on math_errhandling & MATH_ERRNO; underflow reporting is implementation-defined.
Examples
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const char *text = "12.5kg";
char *end;
double value = strtod(text, &end);
if (end == text) {
puts("no conversion");
return EXIT_FAILURE;
}
printf("value=%.1f; suffix=%s\n", value, end);
return EXIT_SUCCESS;
}Result
value=12.5; suffix=kg
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.