]> git.saurik.com Git - apple/libc.git/blame - stdlib/FreeBSD/strtol.3
Libc-1158.50.2.tar.gz
[apple/libc.git] / stdlib / FreeBSD / strtol.3
CommitLineData
5b2abdfb
A
1.\" Copyright (c) 1990, 1991, 1993
2.\" The Regents of the University of California. All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
5b2abdfb
A
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)strtol.3 8.1 (Berkeley) 6/4/93
1f2f436a 33.\" $FreeBSD: src/lib/libc/stdlib/strtol.3,v 1.23 2007/04/10 11:17:00 ru Exp $
5b2abdfb 34.\"
9385eb3d 35.Dd November 28, 2001
5b2abdfb
A
36.Dt STRTOL 3
37.Os
38.Sh NAME
ad3c9f2a
A
39.Nm strtoimax ,
40.Nm strtol ,
41.Nm strtoll ,
42.Nm strtoq
9385eb3d
A
43.Nd "convert a string value to a"
44.Vt long , "long long" , intmax_t
45or
46.Vt quad_t
47integer
5b2abdfb
A
48.Sh LIBRARY
49.Lb libc
50.Sh SYNOPSIS
ad3c9f2a
A
51.In inttypes.h
52.Ft intmax_t
53.Fo strtoimax
54.Fa "const char *restrict str"
55.Fa "char **restrict endptr"
56.Fa "int base"
57.Fc
5b2abdfb 58.In stdlib.h
5b2abdfb 59.Ft long
ad3c9f2a
A
60.Fo strtol
61.Fa "const char *restrict str"
62.Fa "char **restrict endptr"
63.Fa "int base"
64.Fc
5b2abdfb 65.Ft long long
ad3c9f2a
A
66.Fo strtoll
67.Fa "const char *restrict str"
68.Fa "char **restrict endptr"
69.Fa "int base"
70.Fc
5b2abdfb
A
71.In sys/types.h
72.In stdlib.h
73.In limits.h
74.Ft quad_t
ad3c9f2a
A
75.Fo strtoq
76.Fa "const char *str"
77.Fa "char **endptr"
78.Fa "int base"
79.Fc
5b2abdfb
A
80.Sh DESCRIPTION
81The
82.Fn strtol
83function
84converts the string in
ad3c9f2a 85.Fa str
5b2abdfb 86to a
9385eb3d 87.Vt long
5b2abdfb
A
88value.
89The
90.Fn strtoll
91function
92converts the string in
ad3c9f2a 93.Fa str
5b2abdfb 94to a
9385eb3d
A
95.Vt "long long"
96value.
97The
98.Fn strtoimax
99function
100converts the string in
ad3c9f2a 101.Fa str
9385eb3d
A
102to an
103.Vt intmax_t
5b2abdfb
A
104value.
105The
106.Fn strtoq
107function
108converts the string in
ad3c9f2a 109.Fa str
5b2abdfb 110to a
9385eb3d 111.Vt quad_t
5b2abdfb
A
112value.
113The conversion is done according to the given
114.Fa base ,
115which must be between 2 and 36 inclusive,
116or be the special value 0.
117.Pp
118The string may begin with an arbitrary amount of white space
119(as determined by
120.Xr isspace 3 )
121followed by a single optional
122.Ql +
123or
124.Ql -
125sign.
126If
127.Fa base
128is zero or 16,
129the string may then include a
9385eb3d 130.Dq Li 0x
5b2abdfb
A
131prefix,
132and the number will be read in base 16; otherwise, a zero
133.Fa base
134is taken as 10 (decimal) unless the next character is
135.Ql 0 ,
136in which case it is taken as 8 (octal).
137.Pp
138The remainder of the string is converted to a
9385eb3d
A
139.Vt long , "long long" , intmax_t
140or
141.Vt quad_t
5b2abdfb
A
142value in the obvious manner,
143stopping at the first character which is not a valid digit
144in the given base.
145(In bases above 10, the letter
146.Ql A
147in either upper or lower case
148represents 10,
149.Ql B
150represents 11, and so forth, with
151.Ql Z
152representing 35.)
153.Pp
154If
155.Fa endptr
9385eb3d
A
156is not
157.Dv NULL ,
5b2abdfb
A
158.Fn strtol
159stores the address of the first invalid character in
160.Fa *endptr .
161If there were no digits at all, however,
162.Fn strtol
163stores the original value of
ad3c9f2a 164.Fa str
5b2abdfb
A
165in
166.Fa *endptr .
167(Thus, if
ad3c9f2a 168.Fa *str
5b2abdfb
A
169is not
170.Ql \e0
171but
172.Fa **endptr
173is
174.Ql \e0
175on return, the entire string was valid.)
ad3c9f2a
A
176.Pp
177Extended locale versions of these functions are documented in
178.Xr strtol_l 3 .
179See
180.Xr xlocale 3
181for more information.
5b2abdfb
A
182.Sh RETURN VALUES
183The
9385eb3d
A
184.Fn strtol ,
185.Fn strtoll ,
ad3c9f2a 186.Fn strtoimax ,
9385eb3d
A
187and
188.Fn strtoq
189functions
190return the result of the conversion,
5b2abdfb
A
191unless the value would underflow or overflow.
192If no conversion could be performed, 0 is returned and
193the global variable
194.Va errno
195is set to
224c7076
A
196.Er EINVAL
197(the last feature is not portable across all platforms).
9385eb3d 198If an overflow or underflow occurs,
5b2abdfb
A
199.Va errno
200is set to
9385eb3d
A
201.Er ERANGE
202and the function return value is clamped according
203to the following table.
ad3c9f2a 204.Bl -column -offset indent ".Fn strtoimax" ".Dv INTMAX_MIN" ".Dv INTMAX_MAX"
1f2f436a 205.It Sy Function Ta Sy underflow Ta Sy overflow
9385eb3d
A
206.It Fn strtol Ta Dv LONG_MIN Ta Dv LONG_MAX
207.It Fn strtoll Ta Dv LLONG_MIN Ta Dv LLONG_MAX
208.It Fn strtoimax Ta Dv INTMAX_MIN Ta Dv INTMAX_MAX
209.It Fn strtoq Ta Dv LLONG_MIN Ta Dv LLONG_MAX
210.El
5b2abdfb
A
211.Sh ERRORS
212.Bl -tag -width Er
213.It Bq Er EINVAL
214The value of
215.Fa base
216is not supported or
224c7076
A
217no conversion could be performed
218(the last feature is not portable across all platforms).
5b2abdfb
A
219.It Bq Er ERANGE
220The given string was out of range; the value converted has been clamped.
221.El
ad3c9f2a
A
222.Sh LEGACY SYNOPSIS
223.Fd #include <stdlib.h>
224.Fd #include <limits.h>
225.Pp
226.In limits.h
227is necessary for the
228.Fn strtol
229and
230.Fn strtoll
231functions.
5b2abdfb
A
232.Sh SEE ALSO
233.Xr atof 3 ,
234.Xr atoi 3 ,
235.Xr atol 3 ,
236.Xr strtod 3 ,
ad3c9f2a 237.Xr strtol_l 3 ,
9385eb3d 238.Xr strtoul 3 ,
ad3c9f2a
A
239.Xr wcstol 3 ,
240.Xr compat 5
5b2abdfb
A
241.Sh STANDARDS
242The
243.Fn strtol
244function
245conforms to
246.St -isoC .
247The
248.Fn strtoll
9385eb3d
A
249and
250.Fn strtoimax
251functions
252conform to
5b2abdfb
A
253.St -isoC-99 .
254The
255.Bx
256.Fn strtoq
257function is deprecated.