CKTIS / C
strtof — convert text to float
strtof reads the beginning of a string as float. The endptr pointer lets you detect missing conversion and remaining text.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <stdlib.h>
float strtof(const char *restrict nptr, char **restrict endptr);strtof from <stdlib.h> skips leading whitespace and converts a string prefix to float. The decimal separator depends on the locale.
When endptr != NULL, it stores a pointer to the first unconsumed character. end == text means no conversion; zero alone does not establish an error. Full consumption also requires *end == '\0'.
In N3096, range reporting depends on math_errhandling: overflow sets ERANGE when MATH_ERRNO is enabled; setting it for underflow is implementation-defined. Clear errno before checking it.
Examples
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const char *text = " 12.5kg";
char *end;
float value = strtof(text, &end);
if (end == text) {
puts("no conversion");
return 1;
}
printf("value=%.1f; rest=%s\n", (double)value, end);
return 0;
}
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.