1STRTOD(3) Library functions 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
13 #define _XOPEN_SOURCE 600 /* or #define _ISOC99_SOURCE */
14 #include <stdlib.h>
15
16 float strtof(const char *nptr, char **endptr);
17 long double strtold(const char *nptr, char **endptr);
18
20 The strtod(), strtof(), and strtold() functions convert the initial
21 portion of the string pointed to by nptr to double, float, and long
22 double representation, respectively.
23
24 The expected form of the (initial portion of the) string is optional
25 leading white space as recognized by isspace(3), an optional plus
26 (``+'') or minus sign (``-'') and then either (i) a decimal number, or
27 (ii) a hexadecimal number, or (iii) an infinity, or (iv) a NAN (not-a-
28 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 non-empty 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
38 nonempty sequence of hexadecimal digits possibly containing a radix
39 character, optionally followed by a binary exponent. A binary exponent
40 consists of a ``P'' or ``p'', followed by an optional plus or minus
41 sign, followed by a non-empty sequence of decimal digits, and indicates
42 multiplication by a power of 2. At least one of radix character and
43 binary exponent 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
48 sequence of characters, followed by ')'. The character string speci‐
49 fies in an implementation-dependent way the type of NAN.
50
51
53 These functions return the converted value, if any.
54
55 If endptr is not NULL, a pointer to the character after the last char‐
56 acter used in the conversion is stored in the location referenced by
57 endptr.
58
59 If no conversion is performed, zero is returned and the value of nptr
60 is stored in the location referenced by endptr.
61
62 If the correct value would cause overflow, plus or minus HUGE_VAL
63 (HUGE_VALF, HUGE_VALL) is returned (according to the sign of the
64 value), and ERANGE is stored in errno. If the correct value would
65 cause underflow, zero is returned and ERANGE is stored in errno.
66
68 ERANGE Overflow or underflow occurred.
69
71 C89 describes strtod(), C99 describes the other two functions.
72
74 Since 0 can legitimately be returned on both success and failure, the
75 calling program should set errno to 0 before the call, and then deter‐
76 mine if an error occurred by checking whether errno has a non-zero
77 value after the call.
78
80 See the example on the strtol(3) manual page; the use of the functions
81 described in this manual page is similar.
82
84 atof(3), atoi(3), atol(3), strtol(3), strtoul(3), fea‐
85 ture_test_macros(7)
86
87
88
89Linux 2001-06-07 STRTOD(3)