1 .\" $NetBSD: strtol.3,v 1.32 2015/05/01 14:17:56 christos 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
98 The conversion is done according to the given
100 which must be between 2 and 36 inclusive,
101 or be the special value 0.
103 The string may begin with an arbitrary amount of white space
106 followed by a single optional
114 the string may then include a
117 and the number will be read in base 16; otherwise, a zero
119 is taken as 10 (decimal) unless the next character is
121 in which case it is taken as 8 (octal).
123 The remainder of the string is converted to an appropriate value
124 in the obvious manner,
125 stopping at the first character which is not a valid digit in the given base.
126 (In bases above 10, the letter
128 in either upper or lower case
131 represents 11, and so forth, with
137 is non-nil, the functions store the address of the first invalid character in
139 If there were no digits at all, however,
140 the functions store the original value of
152 on return, the entire string was valid.)
157 returns the result of the conversion,
158 unless the value would underflow or overflow.
159 If an underflow occurs,
170 If an overflow occurs,
187 argument is not supported then
191 and the functions return 0.
196 This behavior (which is unlike most library functions) is guaranteed
197 by the pertinent standards.
199 Because the return value of
201 cannot be used unambiguously to detect an error,
203 is left unchanged after a successful call.
204 To ensure that a string is a valid number (i.e., in range and containing no
205 trailing characters), clear
207 beforehand explicitly, then check it afterwards:
208 .Bd -literal -offset indent
215 lval = strtol(buf, \*[Am]ep, 10);
216 if (buf[0] == '\e0' || *ep != '\e0')
218 if (errno == ERANGE \*[Am]\*[Am] (lval == LONG_MAX || lval == LONG_MIN))
222 This example will accept
228 If trailing whitespace is acceptable, further checks must be done on
235 is being used instead of
237 error checking is further complicated because the desired return value is an
241 however, on some architectures integers and long integers are the same size.
242 Thus the following is necessary:
243 .Bd -literal -offset indent
251 lval = strtol(buf, \*[Am]ep, 10);
252 if (buf[0] == '\e0' || *ep != '\e0')
254 if ((errno == ERANGE \*[Am]\*[Am] (lval == LONG_MAX || lval == LONG_MIN)) ||
255 (lval \*[Gt] INT_MAX || lval \*[Lt] INT_MIN))
264 is not between 2 and 36 and does not contain the special value 0.
266 The given string was out of range; the value converted has been clamped.
295 legacy function equivalent to
297 and should not be used in a new code.
299 Ignores the current locale.