Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <stdlib.h>

double strtod(const char *restrict nptr, char **restrict endptr);

Try it

C Parsing with error checks

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.

C Special values and several numbers in one string

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

C
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 == text and errno. As with strtol, assign errno = 0 before the call. A return of 0.0 can mean "the text was 0" or "no conversion".
  • Locale matters. The decimal separator comes from LC_NUMERIC. Programs start in the "C" locale (a dot), but after setlocale(LC_ALL, "") a German locale expects 3,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 same double as the literal 0.1. See floating-point comparison for why that value is not exactly one tenth.
  • inf and nan are accepted, which can surprise validation code. If those must be rejected, test the result with isfinite().
  • 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 (see atoi, atol and atof).
  • Complexity: linear in the length of the number text.