1.\" $NetBSD: strtoul.3,v 1.15 2002/02/07 07:00:30 ross 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: @(#)strtoul.3 8.1 (Berkeley) 6/4/93 39.\" 40.Dd April 26, 2001 41.Dt STRTOUL 3 42.Os 43.Sh NAME 44.Nm strtoul , 45.Nm strtoull , 46.Nm strtoumax , 47.Nm strtouq 48.Nd "convert a string to an unsigned long, unsigned long long, uintmax_t or uquad_t integer" 49.Sh LIBRARY 50.Lb libc 51.Sh SYNOPSIS 52.Fd #include \*[Lt]stdlib.h\*[Gt] 53.Fd #include \*[Lt]limits.h\*[Gt] 54.Ft unsigned long int 55.Fn strtoul "const char * restrict nptr" "char ** restrict endptr" "int base" 56.Ft unsigned long long int 57.Fn strtoull "const char * restrict nptr" "char ** restrict endptr" "int base" 58.Pp 59.Fd #include \*[Lt]inttypes.h\*[Gt] 60.Ft uintmax_t 61.Fn strtoumax "const char * restrict nptr" "char ** restrict endptr" "int base" 62.Pp 63.Fd #include \*[Lt]sys/types.h\*[Gt] 64.Fd #include \*[Lt]stdlib.h\*[Gt] 65.Fd #include \*[Lt]limits.h\*[Gt] 66.Ft u_quad_t 67.Fn strtouq "const char * restrict nptr" "char ** restrict endptr" "int base" 68.Sh DESCRIPTION 69The 70.Fn strtoul 71function 72converts the string in 73.Fa nptr 74to an 75.Em unsigned long int 76value. 77The 78.Fn strtoull 79function 80converts the string in 81.Fa nptr 82to an 83.Em unsigned long long int 84value. 85The 86.Fn strtoumax 87function 88converts the string in 89.Fa nptr 90to an 91.Em uintmax_t 92value. 93The 94.Fn strtouq 95function 96converts the string in 97.Fa nptr 98to a 99.Em u_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 an 127.Em unsigned long 128value in the obvious manner, 129stopping at the end of the string 130or at the first character that does not produce a valid digit 131in the given base. 132(In bases above 10, the letter 133.Ql A 134in either upper or lower case 135represents 10, 136.Ql B 137represents 11, and so forth, with 138.Ql Z 139representing 35.) 140.Pp 141If 142.Fa endptr 143is non nil, 144.Fn strtoul 145stores the address of the first invalid character in 146.Fa *endptr . 147If there were no digits at all, however, 148.Fn strtoul 149stores the original value of 150.Fa nptr 151in 152.Fa *endptr . 153(Thus, if 154.Fa *nptr 155is not 156.Ql \e0 157but 158.Fa **endptr 159is 160.Ql \e0 161on return, the entire string was valid.) 162.Sh RETURN VALUES 163The 164.Fn strtoul 165function 166returns either the result of the conversion 167or, if there was a leading minus sign, 168the negation of the result of the conversion, 169unless the original (non-negated) value would overflow; 170in the latter case, 171.Fn strtoul 172returns 173.Dv ULONG_MAX , 174.Fn strtoull 175returns 176.Dv ULLONG_MAX , 177.Fn strtoumax 178returns 179.Dv UINTMAX_MAX , 180and the global variable 181.Va errno 182is set to 183.Er ERANGE . 184.Sh ERRORS 185.Bl -tag -width Er 186.It Bq Er ERANGE 187The given string was out of range; the value converted has been clamped. 188.El 189.Sh SEE ALSO 190.Xr strtoimax 3 , 191.Xr strtol 3 , 192.Xr strtoll 3 193.Sh STANDARDS 194The 195.Fn strtoul 196function 197conforms to 198.St -ansiC . 199The 200.Fn strtoull 201and 202.Fn strtoumax 203functions conform to 204.St -isoC99 . 205.Sh BUGS 206Ignores the current locale. 207