xref: /netbsd-src/lib/libc/stdlib/strtol.3 (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1.\"	$NetBSD: strtol.3,v 1.20 2003/04/16 13:34:47 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. 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 August 11, 2002
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.In stdlib.h
53.In limits.h
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.In inttypes.h
60.Ft intmax_t
61.Fn strtoimax "const char * restrict nptr" "char ** restrict endptr" "int base"
62.Pp
63.In sys/types.h
64.In stdlib.h
65.In limits.h
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 EXAMPLES
194Ensuring that a string is a valid number (i.e., in range and containing no
195trailing characters) requires clearing
196.Va errno
197beforehand explicitly since
198.Va errno
199is not changed on a successful call to
200.Fn strtol ,
201and the return value of
202.Fn strtol
203cannot be used unambiguously to signal an error:
204.Bd -literal -offset indent
205char *ep;
206long lval;
207
208\&...
209
210errno = 0;
211lval = strtol(buf, &ep, 10);
212if (buf[0] == '\e0' || *ep != '\e0')
213	goto not_a_number;
214if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
215	goto out_of_range;
216.Ed
217.Pp
218This example will accept
219.Dq 12
220but not
221.Dq 12foo
222or
223.Dq 12\en .
224If trailing whitespace is acceptable, further checks must be done on
225.Va *ep ;
226alternately, use
227.Xr sscanf 3 .
228.Pp
229If
230.Fn strtol
231is being used instead of
232.Xr atoi 3 ,
233error checking is further complicated because the desired return value is an
234.Li int
235rather than a
236.Li long ;
237however, on some architectures integers and long integers are the same size.
238Thus the following is necessary:
239.Bd -literal -offset indent
240char *ep;
241int ival;
242long lval;
243
244\&...
245
246errno = 0;
247lval = strtol(buf, &ep, 10);
248if (buf[0] == '\e0' || *ep != '\e0')
249	goto not_a_number;
250if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
251    (lval > INT_MAX || lval < INT_MIN))
252	goto out_of_range;
253ival = lval;
254.Ed
255.Sh ERRORS
256.Bl -tag -width Er
257.It Bq Er ERANGE
258The given string was out of range; the value converted has been clamped.
259.El
260.Sh SEE ALSO
261.Xr atof 3 ,
262.Xr atoi 3 ,
263.Xr atol 3 ,
264.Xr atoll 3 ,
265.Xr strtod 3 ,
266.Xr strtoul 3 ,
267.Xr strtoull 3 ,
268.Xr strtoumax 3
269.Sh STANDARDS
270The
271.Fn strtol
272function
273conforms to
274.St -ansiC .
275The
276.Fn strtoll
277and
278.Fn strtoimax
279functions conform to
280.St -isoC99 .
281.Sh BUGS
282Ignores the current locale.
283