strtod(3)

๐Ÿ’– strtod(3) โ€” string to float ๐Ÿ’–

okay so bestie, welcome to the strtod(3) glow-up page ๐Ÿ’… this lil guy lives in the Library Functions Manual and honestly? underrated. she takes your text and turns it into a real actual floating-point number. no cap.


โœจ NAME โœจ

strtod, strtof, strtold โ€” string to float ๐ŸŽ€

that's it. that's the whole job. text goes in, float comes out.


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc). the classic. she's been there the whole time, holding it down. ๐Ÿ’œ


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <stdlib.h>

double strtod(const char *restrict nptr,
              char **_Nullable restrict endptr);
float strtof(const char *restrict nptr,
             char **_Nullable restrict endptr);
long double strtold(const char *restrict nptr,
                    char **_Nullable restrict endptr);

๐Ÿงš Feature Test Macro Requirements for glibc (see feature_test_macros(7)) ๐Ÿงš

  • strtof(), strtold():
    • _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
like, if you want the float and long double girlies you gotta define one of those. otherwise it's giving... undefined. ๐Ÿซถ

๐Ÿ’… DESCRIPTION ๐Ÿ’…

so strtod(), strtof(), and strtold() all do the same cute thing but for different types: they convert the initial portion of the string pointed to by nptr into double, float, and long double representation respectively. three sisters, one mission. ๐Ÿ‘‘

okay so here's the tea on what that string is allowed to look like. the expected form of the (initial portion of the) string is:

  1. optional leading white space, as recognized by isspace(3) ๐ŸŒฌ๏ธ
  2. an optional plus ('+') or minus sign ('-') โž•โž–
  3. and then one of these four:
    • (i) a decimal number
    • (ii) a hexadecimal number
    • (iii) an infinity
    • (iv) a NAN (not-a-number)

got it? four vibes. let's break each one down because the details matter, bestie. ๐Ÿ”

๐Ÿ“ A decimal number

consists of a nonempty sequence of decimal digits possibly containing a radix character (that's the decimal point, and it's 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 it indicates multiplication by a power of 10. ๐Ÿ”Ÿ

๐Ÿฆ‹ A hexadecimal 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 it indicates multiplication by a power of 2.

โš ๏ธ important!! at least one of radix character and binary exponent must be present. you can't just have the digits and dip. the function will not be fooled. ๐Ÿ˜ค

๐Ÿ‘‘ An infinity

either "INF" or "INFINITY", disregarding case. that's it. no lowercase slander here, case does not matter, she's inclusive. ๐ŸŒˆ

๐Ÿ’€ A NAN

"NAN" (disregarding case again, we don't judge) optionally followed by a string, (n-char-sequence), where n-char-sequence specifies in an implementation-dependent way the type of NAN (see the VERSIONS section down below, bestie, i got u).


๐Ÿ”ฅ RETURN VALUE ๐Ÿ”ฅ

these functions return the converted value, if any. ๐ŸŽ

if endptr is not NULL, a pointer to the character after the last character used in the conversion is stored in the location referenced by endptr. so you can see exactly where she stopped reading. cute right?

if no conversion is performed, zero is returned and (unless endptr is null) the value of nptr is stored in the location referenced by endptr. dramatic but fair. ๐Ÿ˜ญ

if the correct value would cause overflow, plus or minus HUGE_VAL, HUGE_VALF, or HUGE_VALL is returned (according to the return type and sign of the value), and ERANGE is stored in errno. she's like "too much. too big. i cannot." ๐Ÿ’ฅ

if the correct value would cause underflow, a value with magnitude no larger than DBL_MIN, FLT_MIN, or LDBL_MIN is returned and ERANGE is stored in errno. tiny little baby number. ๐Ÿฅบ


๐Ÿšจ ERRORS ๐Ÿšจ

  • ERANGE โ€” Overflow or underflow occurred.

that's the only one. short and iconic. โœจ


๐Ÿ’– ATTRIBUTES ๐Ÿ’–

for an explanation of the terms used in this section, see attributes(7).

  • strtod(), strtof(), strtold() โ€” Thread safety: MT-Safe locale

so she's safe to call from multiple threads, but locale-sensitive, iykyk. ๐ŸŒ


๐ŸŒˆ VERSIONS ๐ŸŒˆ

in the glibc implementation, the n-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. ๐Ÿง 

so yeah, that little suffix is basically a secret payload for the mantissa. not me finding that lowkey fascinating.


โญ STANDARDS โญ

  • C11
  • POSIX.1-2008

certified, up to code, official. periodt. ๐Ÿ’…


๐Ÿ“œ HISTORY ๐Ÿ“œ

  • strtod() โ€” C89, POSIX.1-2001.
  • strtof()
  • strtold() โ€” C99, POSIX.1-2001.

(strtof() and strtold() share that line, bestie โ€” C99 and POSIX.1-2001. the original said it like that, so i say it like that. ๐Ÿซก)


๐Ÿ™ˆ CAVEATS ๐Ÿ™ˆ

since 0 can legitimately be returned on both success and failure, the calling program should set errno to 0 before the call, and then determine if an error occurred by checking whether errno has a nonzero value after the call.

โš ๏ธ do NOT skip this. if you just check the return value you will get played. zero is a liar sometimes. set errno to 0 first, then look at it after. that's the whole ritual. ๐Ÿ•ฏ๏ธ

๐ŸŽ€ EXAMPLES ๐ŸŽ€

see the example on the strtol(3) manual page; the use of the functions described in this manual page is similar. ๐Ÿ‘€ go peek, the vibes transfer over.


๐Ÿฆ„ SEE ALSO ๐Ÿฆ„

atof(3), atoi(3), atol(3), nan(3), nanf(3), nanl(3), strfromd(3), strtol(3), strtoul(3)


and that's the page!! ๐Ÿ’– this little beauty is from Linux man-pages 6.20, dated 2026-07-26 โ€” go off, man-pages maintainers, we love u. ๐Ÿซถ stay converting, bestie, and remember: set that errno to 0. ๐ŸŽ€โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: BSD-4-Clause-UC
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms