C Reference ยท stdlib.h
C strtod() and strtof() functions
Reference for C strtod(), strtof() and strtold(): convert text to floating-point values, detect bad input with endptr and errno, handle inf, nan and hex floats.
strtod() converts the beginning of a string to a double. It is the floating-point counterpart of strtol(): it reports where parsing stopped and signals overflow and underflow through errno. strtof() returns a float and strtold() a long double.
It accepts ordinary decimal notation (3.14, -2e5, .5), hexadecimal floats (0x1.8p1), and the special spellings inf, infinity and nan, in any letter case.
#include <stdlib.h> double strtod(const char *restrict nptr, char **restrict endptr);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
double strtod (const char *restrict nptr, char **restrict endptr); float strtof (const char *restrict nptr, char **restrict endptr); long double strtold(const char *restrict nptr, char **restrict endptr);
| Name | Description |
|---|---|
nptr |
The string to convert. Leading white space is skipped. |
endptr |
If not NULL, receives a pointer to the first character not used. |
Returns: the converted value. If nothing could be converted it returns 0 and sets *endptr to nptr. On overflow it returns HUGE_VAL (or HUGE_VALF/HUGE_VALL), with the sign of the value, and sets errno to ERANGE. On underflow it returns a value no larger than the smallest normal number (possibly 0) and may set errno to ERANGE.
Errors / undefined behavior: nptr must be a valid string. There are no other invalid inputs: garbage simply converts nothing.
Usage notes
- Check
end == textanderrno. As withstrtol, assignerrno = 0before the call. A return of0.0can mean "the text was0" or "no conversion". - Locale matters. The decimal separator comes from
LC_NUMERIC. Programs start in the"C"locale (a dot), but aftersetlocale(LC_ALL, "")a German locale expects3,14. Parse data files in the"C"locale. - Rounding is correct. Conversion from decimal text is exact to the nearest representable value, so
strtod("0.1", NULL)gives the samedoubleas the literal0.1. See floating-point comparison for why that value is not exactly one tenth. infandnanare accepted, which can surprise validation code. If those must be rejected, test the result withisfinite().nan(...)payloads:nan("123")style input is allowed; the meaning of the characters inside is implementation-defined.- Do not use
atof()for untrusted input: it cannot report errors (seeatoi,atolandatof). - Complexity: linear in the length of the number text.