OSDN Git Service

Update README
[linuxjm/LDP_man-pages.git] / original / man3 / strtoul.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References consulted:
26 .\"     Linux libc source code
27 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\"     386BSD man pages
29 .\" Modified Sun Jul 25 10:54:03 1993 by Rik Faith (faith@cs.unc.edu)
30 .\" Fixed typo, aeb, 950823
31 .\" 2002-02-22, joey, mihtjel: Added strtoull()
32 .\"
33 .TH STRTOUL 3  2014-03-18 "GNU" "Linux Programmer's Manual"
34 .SH NAME
35 strtoul, strtoull, strtouq \- convert a string to an unsigned long integer
36 .SH SYNOPSIS
37 .nf
38 .B #include <stdlib.h>
39 .sp
40 .BI "unsigned long int strtoul(const char *" nptr ", char **" endptr \
41 ", int " base );
42 .sp
43 .BI "unsigned long long int strtoull(const char *" nptr ", char **" endptr ,
44 .BI "                                int " base );
45 .fi
46 .sp
47 .in -4n
48 Feature Test Macro Requirements for glibc (see
49 .BR feature_test_macros (7)):
50 .in
51 .sp
52 .ad l
53 .BR strtoull ():
54 .RS 4
55 _XOPEN_SOURCE\ >=\ 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE ||
56 _POSIX_C_SOURCE\ >=\ 200112L;
57 .br
58 or
59 .I cc\ -std=c99
60 .RE
61 .ad
62 .SH DESCRIPTION
63 The
64 .BR strtoul ()
65 function converts the initial part of the string
66 in
67 .I nptr
68 to an
69 .I "unsigned long int"
70 value according to the
71 given
72 .IR base ,
73 which must be between 2 and 36 inclusive, or be
74 the special value 0.
75 .PP
76 The string may begin with an arbitrary amount of white space (as
77 determined by
78 .BR isspace (3))
79 followed by a single optional \(aq+\(aq or \(aq\-\(aq
80 sign.
81 If
82 .I base
83 is zero or 16, the string may then include a
84 "0x" prefix, and the number will be read in base 16; otherwise, a
85 zero
86 .I base
87 is taken as 10 (decimal) unless the next character
88 is \(aq0\(aq, in which case it is taken as 8 (octal).
89 .PP
90 The remainder of the string is converted to an
91 .I "unsigned long int"
92 value in the obvious manner,
93 stopping at the first character which is not a
94 valid digit in the given base.
95 (In bases above 10, the letter \(aqA\(aq in
96 either uppercase or lowercase represents 10, \(aqB\(aq represents 11, and so
97 forth, with \(aqZ\(aq representing 35.)
98 .PP
99 If
100 .I endptr
101 is not NULL,
102 .BR strtoul ()
103 stores the address of the
104 first invalid character in
105 .IR *endptr .
106 If there were no digits at
107 all,
108 .BR strtoul ()
109 stores the original value of
110 .I nptr
111 in
112 .I *endptr
113 (and returns 0).
114 In particular, if
115 .I *nptr
116 is not \(aq\\0\(aq but
117 .I **endptr
118 is \(aq\\0\(aq on return, the entire string is valid.
119 .PP
120 The
121 .BR strtoull ()
122 function works just like the
123 .BR strtoul ()
124 function but returns an
125 .I "unsigned long long int"
126 value.
127 .SH RETURN VALUE
128 The
129 .BR strtoul ()
130 function returns either the result of the conversion
131 or, if there was a leading minus sign, the negation of the result of the
132 conversion represented as an unsigned value,
133 unless the original (nonnegated) value would overflow; in
134 the latter case,
135 .BR strtoul ()
136 returns
137 .B ULONG_MAX
138 and sets
139 .I errno
140 to
141 .BR ERANGE .
142 Precisely the same holds for
143 .BR strtoull ()
144 (with
145 .B ULLONG_MAX
146 instead of
147 .BR ULONG_MAX ).
148 .SH ERRORS
149 .TP
150 .B EINVAL
151 (not in C99)
152 The given
153 .I base
154 contains an unsupported value.
155 .TP
156 .B ERANGE
157 The resulting value was out of range.
158 .LP
159 The implementation may also set
160 .IR errno
161 to
162 .B EINVAL
163 in case
164 no conversion was performed (no digits seen, and 0 returned).
165 .SH ATTRIBUTES
166 .SS Multithreading (see pthreads(7))
167 The
168 .BR strtoul (),
169 .BR strtoull (),
170 and
171 .BR strtouq ()
172 functions are thread-safe with exceptions.
173 These functions can be safely used in multithreaded applications,
174 as long as
175 .BR setlocale (3)
176 is not called to change the locale during their execution.
177 .SH CONFORMING TO
178 .BR strtoul ()
179 conforms to SVr4, C89, C99, and POSIX-2001, and
180 .BR strtoull ()
181 to C99 and POSIX.1-2001.
182 .SH NOTES
183 Since
184 .BR strtoul ()
185 can legitimately return 0 or
186 .B ULONG_MAX
187 .RB ( ULLONG_MAX
188 for
189 .BR strtoull ())
190 on both success and failure, the calling program should set
191 .I errno
192 to 0 before the call,
193 and then determine if an error occurred by checking whether
194 .I errno
195 has a nonzero value after the call.
196
197 In locales other than the "C" locale, other strings may be accepted.
198 (For example, the thousands separator of the current locale may be
199 supported.)
200 .LP
201 BSD also has
202 .sp
203 .in +4n
204 .nf
205 .BI "u_quad_t strtouq(const char *" nptr ", char **" endptr ", int " base );
206 .sp
207 .in -4n
208 .fi
209 with completely analogous definition.
210 Depending on the wordsize of the current architecture, this
211 may be equivalent to
212 .BR strtoull ()
213 or to
214 .BR strtoul ().
215
216 Negative values are considered valid input and are
217 silently converted to the equivalent
218 .I "unsigned long int"
219 value.
220 .SH EXAMPLE
221 See the example on the
222 .BR strtol (3)
223 manual page;
224 the use of the functions described in this manual page is similar.
225 .SH SEE ALSO
226 .BR atof (3),
227 .BR atoi (3),
228 .BR atol (3),
229 .BR strtod (3),
230 .BR strtol (3)
231 .SH COLOPHON
232 This page is part of release 3.79 of the Linux
233 .I man-pages
234 project.
235 A description of the project,
236 information about reporting bugs,
237 and the latest version of this page,
238 can be found at
239 \%http://www.kernel.org/doc/man\-pages/.