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 thefloatandlong doublegirlies 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:
- optional leading white space, as recognized by
isspace(3)๐ฌ๏ธ - an optional plus (
'+') or minus sign ('-') โโ - 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. seterrnoto0first, 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
- Copyright 1990-1991, The Regents of the University of California
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms