xref: /netbsd-src/lib/libc/stdlib/strtol.3 (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1.\"	$NetBSD: strtol.3,v 1.22 2003/09/08 17:54:33 wiz 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. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     from: @(#)strtol.3	8.1 (Berkeley) 6/4/93
35.\"
36.Dd August 11, 2002
37.Dt STRTOL 3
38.Os
39.Sh NAME
40.Nm strtol ,
41.Nm strtoll ,
42.Nm strtoimax ,
43.Nm strtoq
44.Nd "convert string value to a long, long long, intmax_t or quad_t integer"
45.Sh LIBRARY
46.Lb libc
47.Sh SYNOPSIS
48.In stdlib.h
49.In limits.h
50.Ft long int
51.Fn strtol "const char * restrict nptr" "char ** restrict endptr" "int base"
52.Ft long long int
53.Fn strtoll "const char * restrict nptr" "char ** restrict endptr" "int base"
54.Pp
55.In inttypes.h
56.Ft intmax_t
57.Fn strtoimax "const char * restrict nptr" "char ** restrict endptr" "int base"
58.Pp
59.In sys/types.h
60.In stdlib.h
61.In limits.h
62.Ft quad_t
63.Fn strtoq "const char * restrict nptr" "char ** restrict endptr" "int base"
64.Sh DESCRIPTION
65The
66.Fn strtol
67function
68converts the string in
69.Fa nptr
70to a
71.Em long int
72value.
73The
74.Fn strtoll
75function
76converts the string in
77.Fa nptr
78to a
79.Em long long int
80value.
81The
82.Fn strtoimax
83function
84converts the string in
85.Fa nptr
86to an
87.Em intmax_t
88value.
89The
90.Fn strtoq
91function
92converts the string in
93.Fa nptr
94to a
95.Em quad_t
96value.
97The conversion is done according to the given
98.Fa base ,
99which must be between 2 and 36 inclusive,
100or be the special value 0.
101.Pp
102The string may begin with an arbitrary amount of white space
103(as determined by
104.Xr isspace 3 )
105followed by a single optional
106.Ql +
107or
108.Ql -
109sign.
110If
111.Fa base
112is zero or 16,
113the string may then include a
114.Ql 0x
115prefix,
116and the number will be read in base 16; otherwise, a zero
117.Fa base
118is taken as 10 (decimal) unless the next character is
119.Ql 0 ,
120in which case it is taken as 8 (octal).
121.Pp
122The remainder of the string is converted to a
123.Em long
124value in the obvious manner,
125stopping at the first character which is not a valid digit
126in the given base.
127(In bases above 10, the letter
128.Ql A
129in either upper or lower case
130represents 10,
131.Ql B
132represents 11, and so forth, with
133.Ql Z
134representing 35.)
135.Pp
136If
137.Fa endptr
138is non nil,
139.Fn strtol
140stores the address of the first invalid character in
141.Fa *endptr .
142If there were no digits at all, however,
143.Fn strtol
144stores the original value of
145.Fa nptr
146in
147.Fa *endptr .
148(Thus, if
149.Fa *nptr
150is not
151.Ql \e0
152but
153.Fa **endptr
154is
155.Ql \e0
156on return, the entire string was valid.)
157.Sh RETURN VALUES
158The
159.Fn strtol
160function
161returns the result of the conversion,
162unless the value would underflow or overflow.
163If an underflow occurs,
164.Fn strtol
165returns
166.Dv LONG_MIN ,
167.Fn strtoll
168returns
169.Dv LLONG_MIN ,
170and
171.Fn strtoimax
172returns
173.Dv INTMAX_MIN .
174If an overflow occurs,
175.Fn strtol
176returns
177.Dv LONG_MAX ,
178.Fn strtoll
179returns
180.Dv LLONG_MAX ,
181and
182.Fn strtoimax
183returns
184.Dv INTMAX_MAX .
185In these cases,
186.Va errno
187is set to
188.Er ERANGE .
189.Sh EXAMPLES
190Ensuring that a string is a valid number (i.e., in range and containing no
191trailing characters) requires clearing
192.Va errno
193beforehand explicitly since
194.Va errno
195is not changed on a successful call to
196.Fn strtol ,
197and the return value of
198.Fn strtol
199cannot be used unambiguously to signal an error:
200.Bd -literal -offset indent
201char *ep;
202long lval;
203
204\&...
205
206errno = 0;
207lval = strtol(buf, &ep, 10);
208if (buf[0] == '\e0' || *ep != '\e0')
209	goto not_a_number;
210if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
211	goto out_of_range;
212.Ed
213.Pp
214This example will accept
215.Dq 12
216but not
217.Dq 12foo
218or
219.Dq 12\en .
220If trailing whitespace is acceptable, further checks must be done on
221.Va *ep ;
222alternately, use
223.Xr sscanf 3 .
224.Pp
225If
226.Fn strtol
227is being used instead of
228.Xr atoi 3 ,
229error checking is further complicated because the desired return value is an
230.Li int
231rather than a
232.Li long ;
233however, on some architectures integers and long integers are the same size.
234Thus the following is necessary:
235.Bd -literal -offset indent
236char *ep;
237int ival;
238long lval;
239
240\&...
241
242errno = 0;
243lval = strtol(buf, &ep, 10);
244if (buf[0] == '\e0' || *ep != '\e0')
245	goto not_a_number;
246if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
247    (lval > INT_MAX || lval < INT_MIN))
248	goto out_of_range;
249ival = lval;
250.Ed
251.Sh ERRORS
252.Bl -tag -width Er
253.It Bq Er ERANGE
254The given string was out of range; the value converted has been clamped.
255.El
256.Sh SEE ALSO
257.Xr atof 3 ,
258.Xr atoi 3 ,
259.Xr atol 3 ,
260.Xr atoll 3 ,
261.Xr strtod 3 ,
262.Xr strtoul 3 ,
263.Xr strtoull 3 ,
264.Xr strtoumax 3
265.Sh STANDARDS
266The
267.Fn strtol
268function
269conforms to
270.St -ansiC .
271The
272.Fn strtoll
273and
274.Fn strtoimax
275functions conform to
276.St -isoC-99 .
277.Sh BUGS
278Ignores the current locale.
279