CKTIS / C
atof — C23 library function
`atof(const char *nptr)` from `<stdlib.h>` converts the initial portion of a string to `double`; it behaves like `strtod(nptr, NULL)` but provides neither a useful end pointer nor dependable error diagnostics.
Version: C23
Syntax
double atof(const char *nptr);atof(const char *nptr) from <stdlib.h> converts the initial portion of a string to double; it behaves like strtod(nptr, NULL) but provides neither a useful end pointer nor dependable error diagnostics.
Use it only for trusted text whose validity is already established. The example actually passes " -12.5e1tail": whitespace, sign, and exponent are consumed, and conversion stops before tail.
No conversion returns 0.0, which is indistinguishable from a valid zero. In C23, behavior is undefined if the result value cannot be represented, and nptr must not be NULL. Use strtod to validate input, detect range via errno, and locate the first unconsumed character.
Examples
#include <stdio.h>
#include <stdlib.h>
int main(void) {
double value = atof(" -12.5e1tail");
printf("%.1f\n", value);
return 0;
}
Result
-125.0
Use it only for trusted text whose validity is already established. The example actually passes `" -12.5e1tail"`: whitespace, sign, and exponent are consumed, and conversion stops before `tail`.