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