1.\" $NetBSD: strtoul.3,v 1.31 2015/05/03 12:29:28 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: @(#)strtoul.3 8.1 (Berkeley) 6/4/93 35.\" 36.Dd April 30, 2015 37.Dt STRTOUL 3 38.Os 39.Sh NAME 40.Nm strtoul , 41.Nm strtoull , 42.Nm strtoumax , 43.Nm strtouq 44.Nd convert a string to an unsigned long, unsigned long long, uintmax_t or uquad_t integer 45.Sh LIBRARY 46.Lb libc 47.Sh SYNOPSIS 48.In stdlib.h 49.In limits.h 50.Ft unsigned long int 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" 54.Pp 55.In inttypes.h 56.Ft uintmax_t 57.Fn strtoumax "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 u_quad_t 63.Fn strtouq "const char * restrict nptr" "char ** restrict endptr" "int base" 64.Sh DESCRIPTION 65The 66.Fn strtoul 67function 68converts the string in 69.Fa nptr 70to an 71.Ft unsigned long int 72value. 73The 74.Fn strtoull 75function 76converts the string in 77.Fa nptr 78to an 79.Ft unsigned long long int 80value. 81The 82.Fn strtoumax 83function 84converts the string in 85.Fa nptr 86to an 87.Ft uintmax_t 88value. 89The 90.Fn strtouq 91function 92converts the string in 93.Fa nptr 94to a 95.Ft u_quad_t 96value. 97.Pp 98The conversion is done according to the given 99.Fa base , 100which must be between 2 and 36 inclusive, 101or be the special value 0. 102.Pp 103The string may begin with an arbitrary amount of white space 104(as determined by 105.Xr isspace 3 ) 106followed by a single optional 107.Ql + 108or 109.Ql - 110sign. 111If 112.Fa base 113is zero or 16, 114the string may then include a 115.Ql 0x 116prefix, 117and the number will be read in base 16; otherwise, a zero 118.Fa base 119is taken as 10 (decimal) unless the next character is 120.Ql 0 , 121in which case it is taken as 8 (octal). 122.Pp 123The remainder of the string is converted to an appropriate 124value in the obvious manner, 125stopping at the end of the string 126or at the first character that does not produce a valid digit 127in the given base. 128(In bases above 10, the letter 129.Ql A 130in either upper or lower case 131represents 10, 132.Ql B 133represents 11, and so forth, with 134.Ql Z 135representing 35.) 136.Pp 137If 138.Fa endptr 139is non-nil, the functions store the address of the first invalid character in 140.Fa *endptr . 141If there were no digits at all, however, 142the functions store the original value of 143.Fa nptr 144in 145.Fa *endptr . 146(Thus, if 147.Fa *nptr 148is not 149.Ql \e0 150but 151.Fa **endptr 152is 153.Ql \e0 154on return, the entire string was valid.) 155.Sh RETURN VALUES 156The 157.Fn strtoul 158function 159returns either the result of the conversion 160or, if there was a leading minus sign, 161the negation of the result of the conversion, 162unless the original (non-negated) value would overflow; 163in the latter case, 164.Fn strtoul 165returns 166.Dv ULONG_MAX , 167.Fn strtoull 168returns 169.Dv ULLONG_MAX , 170.Fn strtoumax 171returns 172.Dv UINTMAX_MAX , 173.Fn strtouq 174returns 175.Dv UQUAD_MAX , 176and the global variable 177.Va errno 178is set to 179.Er ERANGE . 180.Pp 181There is no way to determine if 182.Fn strtoul 183has processed a negative number (and returned an unsigned value) short of 184examining the string in 185.Fa nptr 186directly. 187If the 188.Fa base 189argument is not supported then 190.Va errno 191is set to 192.Er EINVAL 193and the functions return 0. 194.Pp 195If no error occurs, 196.Va errno 197is left unchanged. 198This behavior (which is unlike most library functions) is guaranteed 199by the pertinent standards. 200.Sh EXAMPLES 201Because the return value of 202.Fn strtoul 203cannot be used unambiguously to detect an error, 204.Va errno 205is left unchanged after a successful call. 206To ensure that a string is a valid number (i.e., in range and containing no 207trailing characters), clear 208.Va errno 209beforehand explicitly, then check it afterwards: 210.Bd -literal -offset indent 211char *ep; 212unsigned long ulval; 213 214\&... 215 216errno = 0; 217ulval = strtoul(buf, \*[Am]ep, 10); 218if (buf[0] == '\e0' || *ep != '\e0') 219 goto not_a_number; 220if (errno == ERANGE \*[Am]\*[Am] ulval == ULONG_MAX) 221 goto out_of_range; 222.Ed 223.Pp 224This example will accept 225.Dq 12 226but not 227.Dq 12foo 228or 229.Dq 12\en . 230If trailing whitespace is acceptable, further checks must be done on 231.Va *ep ; 232alternately, use 233.Xr sscanf 3 . 234.Sh ERRORS 235.Bl -tag -width Er 236.It Bq Er EINVAL 237The 238.Ar base 239is not between 2 and 36 and does not contain the special value 0. 240.It Bq Er ERANGE 241The given string was out of range; the value converted has been clamped. 242.El 243.Sh SEE ALSO 244.Xr atof 3 , 245.Xr atoi 3 , 246.Xr atol 3 , 247.Xr atoll 3 , 248.Xr strtod 3 , 249.Xr strtoi 3 , 250.Xr strtoimax 3 , 251.Xr strtol 3 , 252.Xr strtoll 3 , 253.Xr strtou 3 254.Sh STANDARDS 255The 256.Fn strtoul 257function 258conforms to 259.St -ansiC . 260The 261.Fn strtoull 262and 263.Fn strtoumax 264functions conform to 265.St -isoC-99 . 266.Pp 267The 268.Fn strtouq 269function is a 270.Bx 271legacy function equivalent to 272.Fn strtoull 273and should not be used in a new code. 274.Sh BUGS 275Ignores the current locale. 276