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
33 exponent consists of an 'E' or 'e', followed by an optional plus or
34 minus sign, followed by a nonempty sequence of decimal digits, and
35 indicates 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, HUGE_VALL) is returned (according to the sign of the
63 value), and ERANGE is stored in errno. If the correct value would
64 cause underflow, zero is returned and ERANGE is stored in errno.
65
67 ERANGE Overflow or underflow occurred.
68
70 For an explanation of the terms used in this section, see
71 attributes(7).
72
73 ┌──────────────────────────────┬───────────────┬────────────────┐
74 │Interface │ Attribute │ Value │
75 ├──────────────────────────────┼───────────────┼────────────────┤
76 │strtod(), strtof(), strtold() │ Thread safety │ MT-Safe locale │
77 └──────────────────────────────┴───────────────┴────────────────┘
79 POSIX.1-2001, POSIX.1-2008, C99.
80
81 strtod() was also described in C89.
82
84 Since 0 can legitimately be returned on both success and failure, the
85 calling program should set errno to 0 before the call, and then deter‐
86 mine if an error occurred by checking whether errno has a nonzero value
87 after the call.
88
89 In the glibc implementation, the n-char-sequence that optionally fol‐
90 lows "NAN" is interpreted as an integer number (with an optional '0' or
91 '0x' prefix to select base 8 or 16) that is to be placed in the man‐
92 tissa component of the returned value.
93
95 See the example on the strtol(3) manual page; the use of the functions
96 described in this manual page is similar.
97
99 atof(3), atoi(3), atol(3), nan(3), nanf(3), nanl(3), strfromd(3), str‐
100 tol(3), strtoul(3)
101
103 This page is part of release 4.15 of the Linux man-pages project. A
104 description of the project, information about reporting bugs, and the
105 latest version of this page, can be found at
106 https://www.kernel.org/doc/man-pages/.
107
108
109
110Linux 2017-09-15 STRTOD(3)