Print this page
11620 strtod man page typo equence


  27 .\" The contents of this file are subject to the terms of the
  28 .\" Common Development and Distribution License (the "License").
  29 .\" You may not use this file except in compliance with the License.
  30 .\"
  31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  32 .\" or http://www.opensolaris.org/os/licensing.
  33 .\" See the License for the specific language governing permissions
  34 .\" and limitations under the License.
  35 .\"
  36 .\" When distributing Covered Code, include this CDDL HEADER in each
  37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  38 .\" If applicable, add the following below this CDDL HEADER, with the
  39 .\" fields enclosed by brackets "[]" replaced with your own identifying
  40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
  41 .\"
  42 .\"
  43 .\" Copyright 1989 AT&T
  44 .\" Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
  45 .\" Portions Copyright (c) 2006, Sun Microsystems, Inc.  All Rights Reserved.
  46 .\"
  47 .TH STRTOD 3C "Nov 1, 2003"
  48 .SH NAME
  49 strtod, strtof, strtold, atof \- convert string to floating-point number
  50 .SH SYNOPSIS
  51 .LP
  52 .nf
  53 #include <stdlib.h>
  54 
  55 \fBdouble\fR \fBstrtod\fR(\fBconst char *restrict\fR \fInptr\fR, \fBchar **restrict\fR \fIendptr\fR);
  56 .fi
  57 
  58 .LP
  59 .nf
  60 \fBfloat\fR \fBstrtof\fR(\fBconst char *restrict\fR \fInptr\fR, \fBchar **restrict\fR \fIendptr\fR);
  61 .fi
  62 
  63 .LP
  64 .nf
  65 \fBlong double\fR \fBstrtold\fR(\fBconst char *restrict\fR \fInptr\fR, \fBchar **restrict\fR \fIendptr\fR);
  66 .fi
  67 
  68 .LP
  69 .nf
  70 \fBdouble\fR \fBatof\fR(\fBconst char *\fR\fIstr\fR);
  71 .fi
  72 
  73 .SH DESCRIPTION
  74 .sp
  75 .LP
  76 The \fBstrtod()\fR, \fBstrtof()\fR, and \fBstrtold()\fR functions convert the
  77 initial portion of the string pointed to by \fInptr\fR to \fBdouble\fR,
  78 \fBfloat\fR, and \fBlong double\fR representation, respectively. First they
  79 decompose the input string into three parts:
  80 .RS +4
  81 .TP
  82 1.
  83 An initial, possibly empty, sequence of white-space characters (as specified
  84 by \fBisspace\fR(3C))
  85 .RE
  86 .RS +4
  87 .TP
  88 2.
  89 A subject sequence interpreted as a floating-point constant or representing
  90 infinity or NaN
  91 .RE
  92 .RS +4
  93 .TP
  94 3.
  95 A final string of one or more unrecognized characters, including the


 132 .sp
 133 .in +2
 134 .nf
 135 n-char-sequence:
 136     digit
 137     nondigit
 138     n-char-sequence digit
 139     n-char-sequence nondigit
 140 .fi
 141 .in -2
 142 
 143 .RE
 144 .sp
 145 .LP
 146 In default mode for \fBstrtod()\fR, only decimal, INF/INFINITY, and
 147 NAN/NAN(\fIn-char-sequence\fR) forms are recognized. In C99/SUSv3 mode,
 148 hexadecimal strings are also recognized.
 149 .sp
 150 .LP
 151 In default mode for \fBstrtod()\fR, the \fIn-char-sequence\fR in the
 152 NAN(\fIn-char-equence\fR) form can contain any character except ')' (right
 153 parenthesis) or '\e0' (null).  In C99/SUSv3 mode, the \fIn-char-sequence\fR can
 154 contain only upper and lower case letters, digits, and '_' (underscore).
 155 .sp
 156 .LP
 157 The \fBstrtof()\fR and \fBstrtold()\fR functions always function in
 158 C99/SUSv3-conformant mode.
 159 .sp
 160 .LP
 161 The subject sequence is defined as the longest initial subsequence of the input
 162 string, starting with the first non-white-space character, that is of the
 163 expected form. The subject sequence contains no characters if the input string
 164 is not of the expected form.
 165 .sp
 166 .LP
 167 If the subject sequence has the expected form for a floating-point number, the
 168 sequence of characters starting with the first digit or the decimal-point
 169 character (whichever occurs first) is interpreted as a floating constant of the
 170 C language, except that the radix character is used in place of a period, and
 171 that if neither an exponent part nor a radix character appears in a decimal
 172 floating-point number, or if a binary exponent part does not appear in a


 185 point inexact, underflow, or overflow exceptions as appropriate.
 186 .sp
 187 .LP
 188 The radix character is defined in the program's locale (category
 189 \fBLC_NUMERIC\fR). In the POSIX locale, or in a locale where the radix
 190 character is not defined, the radix character defaults to a period ('.').
 191 .sp
 192 .LP
 193 If the subject sequence is empty or does not have the expected form, no
 194 conversion is performed; the value of \fInptr\fR is stored in the object
 195 pointed to by \fIendptr\fR, provided that \fIendptr\fR is not a null pointer.
 196 .sp
 197 .LP
 198 The \fBstrtod()\fR function does not change the setting of \fBerrno\fR if
 199 successful.
 200 .sp
 201 .LP
 202 The \fBatof(\fR\fIstr\fR\fB)\fR function call is equivalent to
 203 \fBstrtod(\fR\fInptr\fR\fB, (char **)NULL)\fR.
 204 .SH RETURN VALUES
 205 .sp
 206 .LP
 207 Upon successful completion, these functions return the converted value. If no
 208 conversion could be performed, \fB0\fR is returned.
 209 .sp
 210 .LP
 211 If the correct value is outside the range of representable values,
 212 \fB\(+-HUGE_VAL\fR, \fB\(+-HUGE_VALF\fR, or \fB\(+-HUGE_VALL\fR is returned
 213 (according to the sign of the value), a floating point overflow exception is
 214 raised, and \fBerrno\fR is set to \fBERANGE\fR.
 215 .sp
 216 .LP
 217 If the correct value would cause an underflow, the correctly rounded result
 218 (which may be normal, subnormal, or zero) is returned, a floating point
 219 underflow exception is raised, and \fBerrno\fR is set to \fBERANGE\fR.
 220 .SH ERRORS
 221 .sp
 222 .LP
 223 These functions will fail if:
 224 .sp
 225 .ne 2
 226 .na
 227 \fB\fBERANGE\fR\fR
 228 .ad
 229 .RS 10n
 230 The value to be returned would cause overflow or underflow
 231 .RE
 232 
 233 .sp
 234 .LP
 235 These functions may fail if:
 236 .sp
 237 .ne 2
 238 .na
 239 \fB\fBEINVAL\fR\fR
 240 .ad
 241 .RS 10n
 242 No conversion could be performed.
 243 .RE
 244 
 245 .SH USAGE
 246 .sp
 247 .LP
 248 Since 0 is returned on error and is also a valid return on success, an
 249 application wishing to check for error situations should set \fBerrno\fR to 0,
 250 then call \fBstrtod()\fR, \fBstrtof()\fR, or \fBstrtold()\fR, then check
 251 \fBerrno\fR.
 252 .sp
 253 .LP
 254 The changes to \fBstrtod()\fR introduced by the ISO/IEC 9899: 1999 standard can
 255 alter the behavior of well-formed applications complying with the ISO/IEC 9899:
 256 1990 standard and thus earlier versions of IEEE Std 1003.1-200x. One such
 257 example would be:
 258 .sp
 259 .in +2
 260 .nf
 261 int
 262 what_kind_of_number (char *s)
 263 {
 264      char *endp;
 265      double d;
 266      long l;
 267      d = strtod(s, &endp);


 300 It's an integer with value 16
 301 .fi
 302 .in -2
 303 
 304 .sp
 305 .LP
 306 With the ISO/IEC 9899: 1999 standard, the result is:
 307 .sp
 308 .in +2
 309 .nf
 310 It's a float with value 16
 311 .fi
 312 .in -2
 313 
 314 .sp
 315 .LP
 316 The change in behavior is due to the inclusion of floating-point numbers in
 317 hexadecimal notation without requiring that either a decimal point or the
 318 binary exponent be present.
 319 .SH ATTRIBUTES
 320 .sp
 321 .LP
 322 See \fBattributes\fR(5) for descriptions of the following attributes:
 323 .sp
 324 
 325 .sp
 326 .TS
 327 box;
 328 c | c
 329 l | l .
 330 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 331 _
 332 CSI     Enabled
 333 _
 334 Interface Stability     Standard
 335 _
 336 MT-Level        MT-Safe with exceptions
 337 .TE
 338 
 339 .SH SEE ALSO
 340 .sp
 341 .LP
 342 \fBisspace\fR(3C), \fBlocaleconv\fR(3C), \fBscanf\fR(3C), \fBsetlocale\fR(3C),
 343 \fBstrtol\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5)
 344 .SH NOTES
 345 .sp
 346 .LP
 347 The \fBstrtod()\fR and \fBatof()\fR functions can be used safely in
 348 multithreaded applications, as long as \fBsetlocale\fR(3C) is not called to
 349 change the locale.
 350 .sp
 351 .LP
 352 The DESCRIPTION and RETURN VALUES sections above are very similar to the
 353 wording used by the Single UNIX Specification version 2 (SUSv2) and the 1989 C
 354 Standard to describe the behavior of the \fBstrtod()\fR function.  Since some
 355 users have reported that they find the description confusing, the following
 356 notes might be helpful.
 357 .RS +4
 358 .TP
 359 1.
 360 The \fBstrtod()\fR function does not modify the string pointed to by
 361 \fIstr\fR and does not \fBmalloc()\fR space to hold the decomposed portions of
 362 the input string.
 363 .RE
 364 .RS +4
 365 .TP
 366 2.




  27 .\" The contents of this file are subject to the terms of the
  28 .\" Common Development and Distribution License (the "License").
  29 .\" You may not use this file except in compliance with the License.
  30 .\"
  31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  32 .\" or http://www.opensolaris.org/os/licensing.
  33 .\" See the License for the specific language governing permissions
  34 .\" and limitations under the License.
  35 .\"
  36 .\" When distributing Covered Code, include this CDDL HEADER in each
  37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  38 .\" If applicable, add the following below this CDDL HEADER, with the
  39 .\" fields enclosed by brackets "[]" replaced with your own identifying
  40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
  41 .\"
  42 .\"
  43 .\" Copyright 1989 AT&T
  44 .\" Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
  45 .\" Portions Copyright (c) 2006, Sun Microsystems, Inc.  All Rights Reserved.
  46 .\"
  47 .TH STRTOD 3C "Aug 25, 2019"
  48 .SH NAME
  49 strtod, strtof, strtold, atof \- convert string to floating-point number
  50 .SH SYNOPSIS

  51 .nf
  52 #include <stdlib.h>
  53 
  54 \fBdouble\fR \fBstrtod\fR(\fBconst char *restrict\fR \fInptr\fR, \fBchar **restrict\fR \fIendptr\fR);
  55 .fi
  56 
  57 .LP
  58 .nf
  59 \fBfloat\fR \fBstrtof\fR(\fBconst char *restrict\fR \fInptr\fR, \fBchar **restrict\fR \fIendptr\fR);
  60 .fi
  61 
  62 .LP
  63 .nf
  64 \fBlong double\fR \fBstrtold\fR(\fBconst char *restrict\fR \fInptr\fR, \fBchar **restrict\fR \fIendptr\fR);
  65 .fi
  66 
  67 .LP
  68 .nf
  69 \fBdouble\fR \fBatof\fR(\fBconst char *\fR\fIstr\fR);
  70 .fi
  71 
  72 .SH DESCRIPTION


  73 The \fBstrtod()\fR, \fBstrtof()\fR, and \fBstrtold()\fR functions convert the
  74 initial portion of the string pointed to by \fInptr\fR to \fBdouble\fR,
  75 \fBfloat\fR, and \fBlong double\fR representation, respectively. First they
  76 decompose the input string into three parts:
  77 .RS +4
  78 .TP
  79 1.
  80 An initial, possibly empty, sequence of white-space characters (as specified
  81 by \fBisspace\fR(3C))
  82 .RE
  83 .RS +4
  84 .TP
  85 2.
  86 A subject sequence interpreted as a floating-point constant or representing
  87 infinity or NaN
  88 .RE
  89 .RS +4
  90 .TP
  91 3.
  92 A final string of one or more unrecognized characters, including the


 129 .sp
 130 .in +2
 131 .nf
 132 n-char-sequence:
 133     digit
 134     nondigit
 135     n-char-sequence digit
 136     n-char-sequence nondigit
 137 .fi
 138 .in -2
 139 
 140 .RE
 141 .sp
 142 .LP
 143 In default mode for \fBstrtod()\fR, only decimal, INF/INFINITY, and
 144 NAN/NAN(\fIn-char-sequence\fR) forms are recognized. In C99/SUSv3 mode,
 145 hexadecimal strings are also recognized.
 146 .sp
 147 .LP
 148 In default mode for \fBstrtod()\fR, the \fIn-char-sequence\fR in the
 149 NAN(\fIn-char-sequence\fR) form can contain any character except ')' (right
 150 parenthesis) or '\e0' (null).  In C99/SUSv3 mode, the \fIn-char-sequence\fR can
 151 contain only upper and lower case letters, digits, and '_' (underscore).
 152 .sp
 153 .LP
 154 The \fBstrtof()\fR and \fBstrtold()\fR functions always function in
 155 C99/SUSv3-conformant mode.
 156 .sp
 157 .LP
 158 The subject sequence is defined as the longest initial subsequence of the input
 159 string, starting with the first non-white-space character, that is of the
 160 expected form. The subject sequence contains no characters if the input string
 161 is not of the expected form.
 162 .sp
 163 .LP
 164 If the subject sequence has the expected form for a floating-point number, the
 165 sequence of characters starting with the first digit or the decimal-point
 166 character (whichever occurs first) is interpreted as a floating constant of the
 167 C language, except that the radix character is used in place of a period, and
 168 that if neither an exponent part nor a radix character appears in a decimal
 169 floating-point number, or if a binary exponent part does not appear in a


 182 point inexact, underflow, or overflow exceptions as appropriate.
 183 .sp
 184 .LP
 185 The radix character is defined in the program's locale (category
 186 \fBLC_NUMERIC\fR). In the POSIX locale, or in a locale where the radix
 187 character is not defined, the radix character defaults to a period ('.').
 188 .sp
 189 .LP
 190 If the subject sequence is empty or does not have the expected form, no
 191 conversion is performed; the value of \fInptr\fR is stored in the object
 192 pointed to by \fIendptr\fR, provided that \fIendptr\fR is not a null pointer.
 193 .sp
 194 .LP
 195 The \fBstrtod()\fR function does not change the setting of \fBerrno\fR if
 196 successful.
 197 .sp
 198 .LP
 199 The \fBatof(\fR\fIstr\fR\fB)\fR function call is equivalent to
 200 \fBstrtod(\fR\fInptr\fR\fB, (char **)NULL)\fR.
 201 .SH RETURN VALUES


 202 Upon successful completion, these functions return the converted value. If no
 203 conversion could be performed, \fB0\fR is returned.
 204 .sp
 205 .LP
 206 If the correct value is outside the range of representable values,
 207 \fB\(+-HUGE_VAL\fR, \fB\(+-HUGE_VALF\fR, or \fB\(+-HUGE_VALL\fR is returned
 208 (according to the sign of the value), a floating point overflow exception is
 209 raised, and \fBerrno\fR is set to \fBERANGE\fR.
 210 .sp
 211 .LP
 212 If the correct value would cause an underflow, the correctly rounded result
 213 (which may be normal, subnormal, or zero) is returned, a floating point
 214 underflow exception is raised, and \fBerrno\fR is set to \fBERANGE\fR.
 215 .SH ERRORS


 216 These functions will fail if:
 217 .sp
 218 .ne 2
 219 .na
 220 \fBERANGE\fR
 221 .ad
 222 .RS 10n
 223 The value to be returned would cause overflow or underflow
 224 .RE
 225 
 226 .sp
 227 .LP
 228 These functions may fail if:
 229 .sp
 230 .ne 2
 231 .na
 232 \fBEINVAL\fR
 233 .ad
 234 .RS 10n
 235 No conversion could be performed.
 236 .RE
 237 
 238 .SH USAGE


 239 Since 0 is returned on error and is also a valid return on success, an
 240 application wishing to check for error situations should set \fBerrno\fR to 0,
 241 then call \fBstrtod()\fR, \fBstrtof()\fR, or \fBstrtold()\fR, then check
 242 \fBerrno\fR.
 243 .sp
 244 .LP
 245 The changes to \fBstrtod()\fR introduced by the ISO/IEC 9899: 1999 standard can
 246 alter the behavior of well-formed applications complying with the ISO/IEC 9899:
 247 1990 standard and thus earlier versions of IEEE Std 1003.1-200x. One such
 248 example would be:
 249 .sp
 250 .in +2
 251 .nf
 252 int
 253 what_kind_of_number (char *s)
 254 {
 255      char *endp;
 256      double d;
 257      long l;
 258      d = strtod(s, &endp);


 291 It's an integer with value 16
 292 .fi
 293 .in -2
 294 
 295 .sp
 296 .LP
 297 With the ISO/IEC 9899: 1999 standard, the result is:
 298 .sp
 299 .in +2
 300 .nf
 301 It's a float with value 16
 302 .fi
 303 .in -2
 304 
 305 .sp
 306 .LP
 307 The change in behavior is due to the inclusion of floating-point numbers in
 308 hexadecimal notation without requiring that either a decimal point or the
 309 binary exponent be present.
 310 .SH ATTRIBUTES


 311 See \fBattributes\fR(5) for descriptions of the following attributes:
 312 .sp
 313 
 314 .sp
 315 .TS
 316 box;
 317 c | c
 318 l | l .
 319 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 320 _
 321 CSI     Enabled
 322 _
 323 Interface Stability     Standard
 324 _
 325 MT-Level        MT-Safe with exceptions
 326 .TE
 327 
 328 .SH SEE ALSO


 329 \fBisspace\fR(3C), \fBlocaleconv\fR(3C), \fBscanf\fR(3C), \fBsetlocale\fR(3C),
 330 \fBstrtol\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5)
 331 .SH NOTES


 332 The \fBstrtod()\fR and \fBatof()\fR functions can be used safely in
 333 multithreaded applications, as long as \fBsetlocale\fR(3C) is not called to
 334 change the locale.
 335 .sp
 336 .LP
 337 The DESCRIPTION and RETURN VALUES sections above are very similar to the
 338 wording used by the Single UNIX Specification version 2 (SUSv2) and the 1989 C
 339 Standard to describe the behavior of the \fBstrtod()\fR function.  Since some
 340 users have reported that they find the description confusing, the following
 341 notes might be helpful.
 342 .RS +4
 343 .TP
 344 1.
 345 The \fBstrtod()\fR function does not modify the string pointed to by
 346 \fIstr\fR and does not \fBmalloc()\fR space to hold the decomposed portions of
 347 the input string.
 348 .RE
 349 .RS +4
 350 .TP
 351 2.