1STRTOD(3) Linux Programmer's Manual STRTOD(3)
2
3
4
6 strtod, strtof, strtold - convert ASCII string to floating-point number
7
9 #include <stdlib.h>
10
11 double strtod(const char *nptr, char **endptr);
12 float strtof(const char *nptr, char **endptr);
13 long double strtold(const char *nptr, char **endptr);
14
15 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17 strtof(), strtold():
18 _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
19
21 The strtod(), strtof(), and strtold() functions convert the initial
22 portion of the string pointed to by nptr to double, float, and long
23 double representation, respectively.
24
25 The expected form of the (initial portion of the) string is optional
26 leading white space as recognized by isspace(3), an optional plus ('+')
27 or minus sign ('-') and then either (i) a decimal number, or (ii) a
28 hexadecimal number, or (iii) an infinity, or (iv) a NAN (not-a-number).
29
30 A decimal number consists of a nonempty sequence of decimal digits pos‐
31 sibly containing a radix character (decimal point, locale-dependent,
32 usually '.'), optionally followed by a decimal exponent. A decimal ex‐
33 ponent consists of an 'E' or 'e', followed by an optional plus or minus
34 sign, followed by a nonempty sequence of decimal digits, and indicates
35 multiplication by a power of 10.
36
37 A hexadecimal number consists of a "0x" or "0X" followed by a nonempty
38 sequence of hexadecimal digits possibly containing a radix character,
39 optionally followed by a binary exponent. A binary exponent consists
40 of a 'P' or 'p', followed by an optional plus or minus sign, followed
41 by a nonempty sequence of decimal digits, and indicates multiplication
42 by a power of 2. At least one of radix character and binary exponent
43 must be present.
44
45 An infinity is either "INF" or "INFINITY", disregarding case.
46
47 A NAN is "NAN" (disregarding case) optionally followed by a string, (n-
48 char-sequence), where n-char-sequence specifies in an implementation-
49 dependent way the type of NAN (see NOTES).
50
52 These functions return the converted value, if any.
53
54 If endptr is not NULL, a pointer to the character after the last char‐
55 acter used in the conversion is stored in the location referenced by
56 endptr.
57
58 If no conversion is performed, zero is returned and (unless endptr is
59 null) the value of nptr is stored in the location referenced by endptr.
60
61 If the correct value would cause overflow, plus or minus HUGE_VAL,
62 HUGE_VALF, or HUGE_VALL is returned (according to the return type and
63 sign of the value), and ERANGE is stored in errno.
64
65 If the correct value would cause underflow, a value with magnitude no
66 larger than DBL_MIN, FLT_MIN, or LDBL_MIN is returned and ERANGE is
67 stored in errno.
68
70 ERANGE Overflow or underflow occurred.
71
73 For an explanation of the terms used in this section, see at‐
74 tributes(7).
75
76 ┌──────────────────────────────┬───────────────┬────────────────┐
77 │Interface │ Attribute │ Value │
78 ├──────────────────────────────┼───────────────┼────────────────┤
79 │strtod(), strtof(), strtold() │ Thread safety │ MT-Safe locale │
80 └──────────────────────────────┴───────────────┴────────────────┘
82 POSIX.1-2001, POSIX.1-2008, C99.
83
84 strtod() was also described in C89.
85
87 Since 0 can legitimately be returned on both success and failure, the
88 calling program should set errno to 0 before the call, and then deter‐
89 mine if an error occurred by checking whether errno has a nonzero value
90 after the call.
91
92 In the glibc implementation, the n-char-sequence that optionally fol‐
93 lows "NAN" is interpreted as an integer number (with an optional '0' or
94 '0x' prefix to select base 8 or 16) that is to be placed in the man‐
95 tissa component of the returned value.
96
98 See the example on the strtol(3) manual page; the use of the functions
99 described in this manual page is similar.
100
102 atof(3), atoi(3), atol(3), nan(3), nanf(3), nanl(3), strfromd(3), str‐
103 tol(3), strtoul(3)
104
106 This page is part of release 5.10 of the Linux man-pages project. A
107 description of the project, information about reporting bugs, and the
108 latest version of this page, can be found at
109 https://www.kernel.org/doc/man-pages/.
110
111
112
113Linux 2020-11-01 STRTOD(3)