NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ATTRIBUTES |VERSIONS |STANDARDS |HISTORY |CAVEATS |EXAMPLES |SEE ALSO |COLOPHON | |
strtod(3) Library Functions Manualstrtod(3)strtod, strtof, strtold - convert ASCII string to floating-point number
Standard C library (libc,-lc)
#include <stdlib.h>double strtod(const char *restrictnptr,char **_Nullable restrictendptr);float strtof(const char *restrictnptr,char **_Nullable restrictendptr);long double strtold(const char *restrictnptr,char **_Nullable restrictendptr); Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):strtof(),strtold(): _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
Thestrtod(),strtof(), andstrtold() functions convert the initial portion of the string pointed to bynptr todouble,float, andlong double representation, respectively. The expected form of the (initial portion of the) string is optional leading white space as recognized byisspace(3), an optional plus ('+') or minus sign ('-') and then either (i) a decimal number, or (ii) a hexadecimal number, or (iii) an infinity, or (iv) a NAN (not-a-number). Adecimal number consists of a nonempty sequence of decimal digits possibly containing a radix character (decimal point, locale- dependent, usually '.'), optionally followed by a decimal exponent. A decimal exponent consists of an 'E' or 'e', followed by an optional plus or minus sign, followed by a nonempty sequence of decimal digits, and indicates multiplication by a power of 10. Ahexadecimal number consists of a "0x" or "0X" followed by a nonempty sequence of hexadecimal digits possibly containing a radix character, optionally followed by a binary exponent. A binary exponent consists of a 'P' or 'p', followed by an optional plus or minus sign, followed by a nonempty sequence of decimal digits, and indicates multiplication by a power of 2. At least one of radix character and binary exponent must be present. Aninfinity is either "INF" or "INFINITY", disregarding case. ANAN is "NAN" (disregarding case) optionally followed by a string,(n-char-sequence), wheren-char-sequence specifies in an implementation-dependent way the type of NAN (see VERSIONS).These functions return the converted value, if any. Ifendptr is not NULL, a pointer to the character after the last character used in the conversion is stored in the location referenced byendptr. If no conversion is performed, zero is returned and (unlessendptr is null) the value ofnptr is stored in the location referenced byendptr. If the correct value would cause overflow, plus or minusHUGE_VAL,HUGE_VALF, orHUGE_VALLis returned (according to the return type and sign of the value), andERANGEis stored inerrno. If the correct value would cause underflow, a value with magnitude no larger thanDBL_MIN,FLT_MIN, orLDBL_MINis returned andERANGEis stored inerrno.
ERANGEOverflow or underflow occurred.
For an explanation of the terms used in this section, seeattributes(7). ┌───────────────────────────────┬───────────────┬────────────────┐ │Interface│Attribute│Value│ ├───────────────────────────────┼───────────────┼────────────────┤ │strtod(),strtof(),strtold() │ Thread safety │ MT-Safe locale │ └───────────────────────────────┴───────────────┴────────────────┘
In the glibc implementation, then-char-sequence that optionally follows "NAN" is interpreted as an integer number (with an optional '0' or '0x' prefix to select base 8 or 16) that is to be placed in the mantissa component of the returned value.
C11, POSIX.1-2008.
strtod() C89, POSIX.1-2001.strtof()strtold() C99, POSIX.1-2001.
Since 0 can legitimately be returned on both success and failure, the calling program should seterrno to 0 before the call, and then determine if an error occurred by checking whethererrno has a nonzero value after the call.
See the example on thestrtol(3) manual page; the use of the functions described in this manual page is similar.
atof(3),atoi(3),atol(3),nan(3),nanf(3),nanl(3),strfromd(3),strtol(3),strtoul(3)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17strtod(3)Pages that refer to this page:gawk(1), pcpintro(1), pmstore(1), strace(1), atof(3), atoi(3), nan(3), sscanf(3), strfromd(3), strtol(3), strtoul(3), locale(7)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |