CKTIS / C
strtold — convert text to long double
strtold from <stdlib.h> reads an initial number from text as long double. endptr lets you check where conversion stopped.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <stdlib.h>
long double strtold(const char *restrict nptr, char **restrict endptr);strtold skips leading whitespace and recognizes decimal, hexadecimal, and infinity forms. It recognizes NaN forms only if the return type supports quiet NaNs. Otherwise, a NaN input is treated as not having the expected form, and no conversion is performed. The decimal separator depends on the locale.
nptr must point to a null-terminated string. For non-null endptr, the function stores a pointer to the first unconverted character there; passing NULL is allowed. No conversion means zero and *endptr == nptr; zero alone cannot detect failure.
Clear errno before calling. If overflow occurs with the default rounding mode in effect, the function sets errno to ERANGE. This is not guaranteed in other rounding modes. Whether underflow sets errno to ERANGE is implementation-defined. To require complete conversion, pass a non-null endptr and also check **endptr == '\0' after the call. The example deliberately leaves kg.
Examples
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const char *text = "12.5kg";
char *end;
errno = 0;
long double value = strtold(text, &end);
if (end == text || errno == ERANGE) {
puts("conversion failed");
return EXIT_FAILURE;
}
printf("value=%.1Lf\nrest=%s\n", value, end);
return EXIT_SUCCESS;
}
Result
value=12.5 rest=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.