OSDN Git Service

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