1 .\" $NetBSD: strtol.3,v 1.26 2009/07/23 13:38:57 wiz Exp $
3 .\" Copyright (c) 1990, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
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.
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
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.
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
34 .\" from: @(#)strtol.3 8.1 (Berkeley) 6/4/93
44 .Nd convert string value to a long, long long, intmax_t or quad_t integer
51 .Fn strtol "const char * restrict nptr" "char ** restrict endptr" "int base"
53 .Fn strtoll "const char * restrict nptr" "char ** restrict endptr" "int base"
57 .Fn strtoimax "const char * restrict nptr" "char ** restrict endptr" "int base"
63 .Fn strtoq "const char * restrict nptr" "char ** restrict endptr" "int base"
68 converts the string in
76 converts the string in
84 converts the string in
92 converts the string in
97 The conversion is done according to the given
99 which must be between 2 and 36 inclusive,
100 or be the special value 0.
102 The string may begin with an arbitrary amount of white space
105 followed by a single optional
113 the string may then include a
116 and the number will be read in base 16; otherwise, a zero
118 is taken as 10 (decimal) unless the next character is
120 in which case it is taken as 8 (octal).
122 The remainder of the string is converted to a
124 value in the obvious manner,
125 stopping at the first character which is not a valid digit
127 (In bases above 10, the letter
129 in either upper or lower case
132 represents 11, and so forth, with
140 stores the address of the first invalid character in
142 If there were no digits at all, however,
144 stores the original value of
156 on return, the entire string was valid.)
161 returns the result of the conversion,
162 unless the value would underflow or overflow.
163 If an underflow occurs,
174 If an overflow occurs,
191 argument is not supported then
195 and the functions return 0.
200 This behavior (which is unlike most library functions) is guaranteed
201 by the pertinent standards.
203 Because the return value of
205 cannot be used unambiguously to detect an error,
207 is left unchanged after a successful call.
208 To ensure that a string is a valid number (i.e., in range and containing no
209 trailing characters), clear
211 beforehand explicitly, then check it afterwards:
212 .Bd -literal -offset indent
219 lval = strtol(buf, \*[Am]ep, 10);
220 if (buf[0] == '\e0' || *ep != '\e0')
222 if (errno == ERANGE \*[Am]\*[Am] (lval == LONG_MAX || lval == LONG_MIN))
226 This example will accept
232 If trailing whitespace is acceptable, further checks must be done on
239 is being used instead of
241 error checking is further complicated because the desired return value is an
245 however, on some architectures integers and long integers are the same size.
246 Thus the following is necessary:
247 .Bd -literal -offset indent
255 lval = strtol(buf, \*[Am]ep, 10);
256 if (buf[0] == '\e0' || *ep != '\e0')
258 if ((errno == ERANGE \*[Am]\*[Am] (lval == LONG_MAX || lval == LONG_MIN)) ||
259 (lval \*[Gt] INT_MAX || lval \*[Lt] INT_MIN))
268 is not between 2 and 36 and does not contain the special value 0.
270 The given string was out of range; the value converted has been clamped.
294 Ignores the current locale.