xref: /original-bsd/lib/libc/stdlib/strtoul.3 (revision 56b48dd2)
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek.
6.\" %sccs.include.redist.man%
7.\"
8.\"     @(#)strtoul.3	5.2 (Berkeley) 04/20/91
9.\"
10.Dd
11.Dt STRTOUL 3
12.Os
13.Sh NAME
14.Nm strtoul
15.Nd convert a string to an unsigned long integer
16.Sh SYNOPSIS
17.Fd #include <stdlib.h>
18.Fd #include <limits.h>
19.Fn strtoul "const char *nptr" "char **endptr" "int base"
20.Sh DESCRIPTION
21The
22.Fn strtoul
23function
24converts the string in
25.Fa nptr
26to an
27.Em unsigned long
28value according to the given
29.Fa base ,
30which must be between 2 and 36 inclusive,
31or be the special value 0.
32.Pp
33The string may begin with an arbitrary amount of white space
34(as determined by
35.Xr isspace 3 )
36followed by a single optional
37.Ql +
38or
39.Ql -
40sign.
41If
42.Fa base
43is zero or 16,
44the string may then include a
45.Ql 0x
46prefix,
47and the number will be read in base 16; otherwise, a zero
48.Fa base
49is taken as 10 (decimal) unless the next character is
50.Ql 0 ,
51in which case it is taken as 8 (octal).
52.Pp
53The remainder of the string is converted to an
54.Em unsigned long
55value in the obvious manner,
56stopping at the end of the string
57or at the first character that does not produce a valid digit
58in the given base.
59(In bases above 10, the letter
60.Ql A
61in either upper or lower case
62represents 10,
63.Ql B
64represents 11, and so forth, with
65.Ql Z
66representing 35.)
67.Pp
68If
69.Fa endptr
70is non nil,
71.Fn strtoul
72stores the address of the first invalid character in
73.Fa *endptr .
74If there were no digits at all, however,
75.Fn strtoul
76stores the original value of
77.Fa nptr
78in
79.Fa *endptr .
80(Thus, if
81.Fa *nptr
82is not
83.Ql \e0
84but
85.Fa **endptr
86is
87.Ql \e0
88on return, the entire string was valid.)
89.Sh RETURN VALUES
90The
91.Fn strtoul
92function
93returns either the result of the conversion
94or, if there was a leading minus sign,
95the negation of the result of the conversion,
96unless the original (non-negated) value would overflow;
97in the latter case,
98.Fn strtoul
99returns
100.Dv ULONG_MAX
101and sets the global variable
102.Va errno
103to
104.Er ERANGE .
105.Sh ERRORS
106.Bl -tag -width [ERANGE]
107.It Bq Er ERANGE
108The given string was out of range; the value converted has been clamped.
109.El
110.Sh SEE ALSO
111.Xr strtol 3
112.Sh STANDARDS
113The
114.Fn strtoul
115function
116conforms to
117.St -ansiC .
118.Sh BUGS
119Ignores the current locale.
120