1STRTOD(3P)                 POSIX Programmer's Manual                STRTOD(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10
11

NAME

13       strtod, strtof, strtold — convert a string to a double-precision number
14

SYNOPSIS

16       #include <stdlib.h>
17
18       double strtod(const char *restrict nptr, char **restrict endptr);
19       float strtof(const char *restrict nptr, char **restrict endptr);
20       long double strtold(const char *restrict nptr, char **restrict endptr);
21

DESCRIPTION

23       The functionality described on this reference page is aligned with  the
24       ISO C  standard.  Any  conflict between the requirements described here
25       and the ISO C standard is unintentional. This  volume  of  POSIX.1‐2008
26       defers to the ISO C standard.
27
28       These functions shall convert the initial portion of the string pointed
29       to by nptr to double, float, and long  double  representation,  respec‐
30       tively. First, they decompose the input string into three parts:
31
32        1. An  initial, possibly empty, sequence of white-space characters (as
33           specified by isspace())
34
35        2. A subject sequence interpreted as a floating-point constant or rep‐
36           resenting infinity or NaN
37
38        3. A  final  string  of one or more unrecognized characters, including
39           the terminating NUL character of the input string
40
41       Then they shall attempt to convert the subject sequence to a  floating-
42       point number, and return the result.
43
44       The  expected  form  of  the subject sequence is an optional '+' or '−'
45       sign, then one of the following:
46
47        *  A non-empty sequence of  decimal  digits  optionally  containing  a
48           radix  character;  then an optional exponent part consisting of the
49           character 'e' or the character 'E', optionally followed by a '+' or
50           '−' character, and then followed by one or more decimal digits
51
52        *  A 0x or 0X, then a non-empty sequence of hexadecimal digits option‐
53           ally containing a radix character; then an optional binary exponent
54           part  consisting of the character 'p' or the character 'P', option‐
55           ally followed by a '+' or '−' character, and then followed  by  one
56           or more decimal digits
57
58        *  One of INF or INFINITY, ignoring case
59
60        *  One  of  NAN  or  NAN(n-char-sequenceopt), ignoring case in the NAN
61           part, where:
62
63               n-char-sequence:
64                   digit
65                   nondigit
66                   n-char-sequence digit
67                   n-char-sequence nondigit
68
69       The subject sequence is defined as the longest initial  subsequence  of
70       the  input  string,  starting with the first non-white-space character,
71       that is of the expected form. The subject sequence contains no  charac‐
72       ters if the input string is not of the expected form.
73
74       If the subject sequence has the expected form for a floating-point num‐
75       ber, the sequence of characters starting with the first  digit  or  the
76       decimal-point  character  (whichever occurs first) shall be interpreted
77       as a floating constant of the C language, except that the radix charac‐
78       ter shall be used in place of a period, and that if neither an exponent
79       part nor a radix character appears in a decimal floating-point  number,
80       or if a binary exponent part does not appear in a hexadecimal floating-
81       point number, an exponent part of the appropriate type with value  zero
82       is  assumed  to  follow  the  last  digit in the string. If the subject
83       sequence begins with a minus-sign, the sequence shall be interpreted as
84       negated.  A  character sequence INF or INFINITY shall be interpreted as
85       an infinity, if representable in the return type, else as if it were  a
86       floating constant that is too large for the range of the return type. A
87       character sequence NAN or NAN(n-char-sequenceopt) shall be  interpreted
88       as  a  quiet NaN, if supported in the return type, else as if it were a
89       subject sequence part that does not have the expected form; the meaning
90       of  the  n-char  sequences  is implementation-defined. A pointer to the
91       final string is stored in the object pointed  to  by  endptr,  provided
92       that endptr is not a null pointer.
93
94       If  the  subject  sequence  has the hexadecimal form and FLT_RADIX is a
95       power of 2, the  value  resulting  from  the  conversion  is  correctly
96       rounded.
97
98       The  radix  character  is  defined  in  the  current  locale  (category
99       LC_NUMERIC).  In the POSIX locale, or in a locale where the radix char‐
100       acter  is  not defined, the radix character shall default to a <period>
101       ('.').
102
103       In other than the C or POSIX locales, other implementation-defined sub‐
104       ject sequences may be accepted.
105
106       If the subject sequence is empty or does not have the expected form, no
107       conversion shall be performed; the value  of  nptr  is  stored  in  the
108       object  pointed  to  by  endptr,  provided  that  endptr  is not a null
109       pointer.
110
111       These functions shall not change the setting of errno if successful.
112
113       Since 0 is returned on error and is also a valid return on success,  an
114       application  wishing  to check for error situations should set errno to
115       0, then call strtod(), strtof(), or strtold(), then check errno.
116

RETURN VALUE

118       Upon successful completion, these functions shall return the  converted
119       value.  If  no  conversion could be performed, 0 shall be returned, and
120       errno may be set to [EINVAL].
121
122       If the correct value is outside  the  range  of  representable  values,
123       ±HUGE_VAL,  ±HUGE_VALF,  or  ±HUGE_VALL shall be returned (according to
124       the sign of the value), and errno shall be set to [ERANGE].
125
126       If the correct value would cause an underflow, a value whose  magnitude
127       is  no  greater  than  the  smallest  normalized positive number in the
128       return type shall be returned and errno set to [ERANGE].
129

ERRORS

131       These functions shall fail if:
132
133       ERANGE The value to be returned would cause overflow or underflow.
134
135       These functions may fail if:
136
137       EINVAL No conversion could be performed.
138
139       The following sections are informative.
140

EXAMPLES

142       None.
143

APPLICATION USAGE

145       If the subject sequence has the hexadecimal form and FLT_RADIX is not a
146       power  of  2,  and  the result is not exactly representable, the result
147       should be one of the two numbers in  the  appropriate  internal  format
148       that  are  adjacent  to the hexadecimal floating source value, with the
149       extra stipulation that the error should have a  correct  sign  for  the
150       current rounding direction.
151
152       If  the  subject  sequence has the decimal form and at most DECIMAL_DIG
153       (defined in <float.h>) significant digits, the result  should  be  cor‐
154       rectly rounded. If the subject sequence D has the decimal form and more
155       than DECIMAL_DIG significant digits, consider the two  bounding,  adja‐
156       cent  decimal strings L and U, both having DECIMAL_DIG significant dig‐
157       its, such that the values of L, D, and U satisfy L  <=  D  <=  U.   The
158       result  should  be  one of the (equal or adjacent) values that would be
159       obtained by correctly rounding L and U according to the current  round‐
160       ing  direction,  with the extra stipulation that the error with respect
161       to D should have a correct sign for the current rounding direction.
162
163       The changes to strtod() introduced by  the  ISO/IEC 9899:1999  standard
164       can  alter  the behavior of well-formed applications complying with the
165       ISO/IEC 9899:1990 standard and thus earlier versions of this  standard.
166       One such example would be:
167
168           int
169           what_kind_of_number (char *s)
170           {
171               char *endp;
172               double d;
173               long l;
174
175               d = strtod(s, &endp);
176               if (s != endp && *endp == `\0')
177                   printf("It's a float with value %g\n", d);
178               else
179               {
180                   l = strtol(s, &endp, 0);
181                   if (s != endp && *endp == `\0')
182                       printf("It's an integer with value %ld\n", 1);
183                   else
184                       return 1;
185               }
186               return 0;
187           }
188
189       If the function is called with:
190
191           what_kind_of_number ("0x10")
192
193       an  ISO/IEC 9899:1990  standard-compliant  library  will  result in the
194       function printing:
195
196           It's an integer with value 16
197
198       With the ISO/IEC 9899:1999 standard, the result is:
199
200           It's a float with value 16
201
202       The change in behavior is due to the inclusion of  floating-point  num‐
203       bers  in  hexadecimal  notation without requiring that either a decimal
204       point or the binary exponent be present.
205

RATIONALE

207       None.
208

FUTURE DIRECTIONS

210       None.
211

SEE ALSO

213       fscanf(), isspace(), localeconv(), setlocale(), strtol()
214
215       The  Base  Definitions  volume  of  POSIX.1‐2008,  Chapter  7,  Locale,
216       <float.h>, <stdlib.h>
217
219       Portions  of  this text are reprinted and reproduced in electronic form
220       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
221       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
222       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
223       cal  and  Electronics  Engineers,  Inc  and  The  Open Group.  (This is
224       POSIX.1-2008 with the 2013 Technical Corrigendum  1  applied.)  In  the
225       event of any discrepancy between this version and the original IEEE and
226       The Open Group Standard, the original IEEE and The Open Group  Standard
227       is  the  referee document. The original Standard can be obtained online
228       at http://www.unix.org/online.html .
229
230       Any typographical or formatting errors that appear  in  this  page  are
231       most likely to have been introduced during the conversion of the source
232       files to man page format. To report such errors,  see  https://www.ker
233       nel.org/doc/man-pages/reporting_bugs.html .
234
235
236
237IEEE/The Open Group                  2013                           STRTOD(3P)
Impressum