]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/strtoul.3
9d73da7711a29269453f2b8d5cfc078654232cb8
[thirdparty/man-pages.git] / man3 / strtoul.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\" 386BSD man pages
27 .\" Modified Sun Jul 25 10:54:03 1993 by Rik Faith (faith@cs.unc.edu)
28 .\" Fixed typo, aeb, 950823
29 .\" 2002-02-22, joey, mihtjel: Added strtoull()
30 .\"
31 .TH STRTOUL 3 2002-05-30 "GNU" "Linux Programmer's Manual"
32 .SH NAME
33 strtoul, strtoull, strtouq \- convert a string to an unsigned long integer
34 .SH SYNOPSIS
35 .nf
36 .B #include <stdlib.h>
37 .sp
38 .BI "unsigned long int"
39 .BI "strtoul(const char *" nptr ", char **" endptr ", int " base );
40 .sp
41 .BI "unsigned long long int"
42 .BI "strtoull(const char *" nptr ", char **" endptr ", int " base );
43 .fi
44 .SH DESCRIPTION
45 The
46 .BR strtoul ()
47 function converts the initial part of the string
48 in \fInptr\fP to an unsigned long integer value according to the
49 given \fIbase\fP, which must be between 2 and 36 inclusive, or be
50 the special value 0.
51 .PP
52 The string may begin with an arbitrary amount of white space (as
53 determined by
54 .BR isspace (3))
55 followed by a single optional `+' or `\-'
56 sign.
57 If \fIbase\fP is zero or 16, the string may then include a
58 `0x' prefix, and the number will be read in base 16; otherwise, a
59 zero \fIbase\fP is taken as 10 (decimal) unless the next character
60 is `0', in which case it is taken as 8 (octal).
61 .PP
62 The remainder of the string is converted to an unsigned long int value
63 in the obvious manner, stopping at the first character which is not a
64 valid digit in the given base.
65 (In bases above 10, the letter `A' in
66 either upper or lower case represents 10, `B' represents 11, and so
67 forth, with `Z' representing 35.)
68 .PP
69 If \fIendptr\fP is not NULL,
70 .BR strtoul ()
71 stores the address of the
72 first invalid character in \fI*endptr\fP.
73 If there were no digits at
74 all,
75 .BR strtoul ()
76 stores the original value of \fInptr\fP in
77 \fI*endptr\fP (and returns 0).
78 In particular, if \fI*nptr\fP is not `\\0' but \fI**endptr\fP
79 is `\\0' on return, the entire string is valid.
80 .PP
81 The
82 .BR strtoull ()
83 function works just like the
84 .BR strtoul ()
85 function but returns an unsigned long long integer value.
86 .SH "RETURN VALUE"
87 The
88 .BR strtoul ()
89 function returns either the result of the conversion
90 or, if there was a leading minus sign, the negation of the result of the
91 conversion represented as an unsigned value,
92 unless the original (non-negated) value would overflow; in
93 the latter case,
94 .BR strtoul ()
95 returns ULONG_MAX and sets the global
96 variable \fIerrno\fP to ERANGE.
97 Precisely the same holds for
98 .BR strtoull ()
99 (with ULLONG_MAX instead of ULONG_MAX).
100 .SH ERRORS
101 .TP
102 .B EINVAL
103 (not in C99)
104 The given
105 .I base
106 contains an unsupported value.
107 .TP
108 .B ERANGE
109 The resulting value was out of range.
110 .LP
111 The implementation may also set \fIerrno\fP to \fBEINVAL\fP in case
112 no conversion was performed (no digits seen, and 0 returned).
113 .SH "CONFORMING TO"
114 .BR strtoul ()
115 conforms to SVr4, C89, C99 and POSIX-2001, and
116 .BR strtoull ()
117 to C99 and POSIX.1-2001.
118 .SH NOTES
119 Since
120 .BR strtoul ()
121 can legitimately return 0 or LONG_MAX (LLONG_MAX for
122 .BR strtoull ())
123 on both success and failure, the calling program should set
124 .I errno
125 to 0 before the call,
126 and then determine if an error occurred by checking whether
127 .I errno
128 has a non-zero value after the call.
129
130 In locales other than the "C" locale, other strings may be accepted.
131 (For example, the thousands separator of the current locale may be
132 supported.)
133 .LP
134 BSD also has
135 .sp
136 .in +4n
137 .nf
138 .BI "u_quad_t"
139 .BI "strtouq(const char *" nptr ", char **" endptr ", int base" );
140 .sp
141 .in -4n
142 .fi
143 with completely analogous definition.
144 Depending on the wordsize of the current architecture, this
145 may be equivalent to
146 .BR strtoull ()
147 or to
148 .BR strtoul ().
149
150 Negative values are considered valid input and are
151 silently converted to the equivalent unsigned long value.
152 .SH EXAMPLE
153 See the example on the
154 .BR strtol (3)
155 manual page;
156 the use of the functions described in this manual page is similar.
157 .SH "SEE ALSO"
158 .BR atof (3),
159 .BR atoi (3),
160 .BR atol (3),
161 .BR strtod (3),
162 .BR strtol (3)