1 .\" $NetBSD: strtoul.3,v 1.31 2015/05/03 12:29:28 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: @(#)strtoul.3 8.1 (Berkeley) 6/4/93
44 .Nd convert a string to an unsigned long, unsigned long long, uintmax_t or uquad_t integer
51 .Fn strtoul "const char * restrict nptr" "char ** restrict endptr" "int base"
52 .Ft unsigned long long int
53 .Fn strtoull "const char * restrict nptr" "char ** restrict endptr" "int base"
57 .Fn strtoumax "const char * restrict nptr" "char ** restrict endptr" "int base"
63 .Fn strtouq "const char * restrict nptr" "char ** restrict endptr" "int base"
68 converts the string in
76 converts the string in
79 .Ft unsigned long long int
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
124 value in the obvious manner,
125 stopping at the end of the string
126 or at the first character that does not produce a valid digit
128 (In bases above 10, the letter
130 in either upper or lower case
133 represents 11, and so forth, with
139 is non-nil, the functions store the address of the first invalid character in
141 If there were no digits at all, however,
142 the functions store the original value of
154 on return, the entire string was valid.)
159 returns either the result of the conversion
160 or, if there was a leading minus sign,
161 the negation of the result of the conversion,
162 unless the original (non-negated) value would overflow;
176 and the global variable
181 There is no way to determine if
183 has processed a negative number (and returned an unsigned value) short of
184 examining the string in
189 argument is not supported then
193 and the functions return 0.
198 This behavior (which is unlike most library functions) is guaranteed
199 by the pertinent standards.
201 Because the return value of
203 cannot be used unambiguously to detect an error,
205 is left unchanged after a successful call.
206 To ensure that a string is a valid number (i.e., in range and containing no
207 trailing characters), clear
209 beforehand explicitly, then check it afterwards:
210 .Bd -literal -offset indent
217 ulval = strtoul(buf, \*[Am]ep, 10);
218 if (buf[0] == '\e0' || *ep != '\e0')
220 if (errno == ERANGE \*[Am]\*[Am] ulval == ULONG_MAX)
224 This example will accept
230 If trailing whitespace is acceptable, further checks must be done on
239 is not between 2 and 36 and does not contain the special value 0.
241 The given string was out of range; the value converted has been clamped.
271 legacy function equivalent to
273 and should not be used in a new code.
275 Ignores the current locale.