OSDN Git Service

(split) LDP: Update original to LDP v3.50.
[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  2011-09-15 "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 \fInptr\fP to an
67 .I "unsigned long int"
68 value according to the
69 given \fIbase\fP, which must be between 2 and 36 inclusive, or be
70 the special value 0.
71 .PP
72 The string may begin with an arbitrary amount of white space (as
73 determined by
74 .BR isspace (3))
75 followed by a single optional \(aq+\(aq or \(aq\-\(aq
76 sign.
77 If \fIbase\fP is zero or 16, the string may then include a
78 "0x" prefix, and the number will be read in base 16; otherwise, a
79 zero \fIbase\fP is taken as 10 (decimal) unless the next character
80 is \(aq0\(aq, in which case it is taken as 8 (octal).
81 .PP
82 The remainder of the string is converted to an
83 .I "unsigned long int"
84 value in the obvious manner,
85 stopping at the first character which is not a
86 valid digit in the given base.
87 (In bases above 10, the letter \(aqA\(aq in
88 either upper or lower case represents 10, \(aqB\(aq represents 11, and so
89 forth, with \(aqZ\(aq representing 35.)
90 .PP
91 If \fIendptr\fP is not NULL,
92 .BR strtoul ()
93 stores the address of the
94 first invalid character in \fI*endptr\fP.
95 If there were no digits at
96 all,
97 .BR strtoul ()
98 stores the original value of \fInptr\fP in
99 \fI*endptr\fP (and returns 0).
100 In particular, if \fI*nptr\fP is not \(aq\\0\(aq but \fI**endptr\fP
101 is \(aq\\0\(aq on return, the entire string is valid.
102 .PP
103 The
104 .BR strtoull ()
105 function works just like the
106 .BR strtoul ()
107 function but returns an
108 .I "unsigned long long int"
109 value.
110 .SH RETURN VALUE
111 The
112 .BR strtoul ()
113 function returns either the result of the conversion
114 or, if there was a leading minus sign, the negation of the result of the
115 conversion represented as an unsigned value,
116 unless the original (nonnegated) value would overflow; in
117 the latter case,
118 .BR strtoul ()
119 returns
120 .B ULONG_MAX
121 and sets \fIerrno\fP to
122 .BR ERANGE .
123 Precisely the same holds for
124 .BR strtoull ()
125 (with
126 .B ULLONG_MAX
127 instead of
128 .BR ULONG_MAX ).
129 .SH ERRORS
130 .TP
131 .B EINVAL
132 (not in C99)
133 The given
134 .I base
135 contains an unsupported value.
136 .TP
137 .B ERANGE
138 The resulting value was out of range.
139 .LP
140 The implementation may also set \fIerrno\fP to \fBEINVAL\fP in case
141 no conversion was performed (no digits seen, and 0 returned).
142 .SH CONFORMING TO
143 .BR strtoul ()
144 conforms to SVr4, C89, C99 and POSIX-2001, and
145 .BR strtoull ()
146 to C99 and POSIX.1-2001.
147 .SH NOTES
148 Since
149 .BR strtoul ()
150 can legitimately return 0 or
151 .B ULONG_MAX
152 .RB ( ULLONG_MAX
153 for
154 .BR strtoull ())
155 on both success and failure, the calling program should set
156 .I errno
157 to 0 before the call,
158 and then determine if an error occurred by checking whether
159 .I errno
160 has a nonzero value after the call.
161
162 In locales other than the "C" locale, other strings may be accepted.
163 (For example, the thousands separator of the current locale may be
164 supported.)
165 .LP
166 BSD also has
167 .sp
168 .in +4n
169 .nf
170 .BI "u_quad_t strtouq(const char *" nptr ", char **" endptr ", int " base );
171 .sp
172 .in -4n
173 .fi
174 with completely analogous definition.
175 Depending on the wordsize of the current architecture, this
176 may be equivalent to
177 .BR strtoull ()
178 or to
179 .BR strtoul ().
180
181 Negative values are considered valid input and are
182 silently converted to the equivalent
183 .I "unsigned long int"
184 value.
185 .SH EXAMPLE
186 See the example on the
187 .BR strtol (3)
188 manual page;
189 the use of the functions described in this manual page is similar.
190 .SH SEE ALSO
191 .BR atof (3),
192 .BR atoi (3),
193 .BR atol (3),
194 .BR strtod (3),
195 .BR strtol (3)