]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/strtoul.3
prctl.2: Clarify the unsupported hardware case of EINVAL
[thirdparty/man-pages.git] / man3 / strtoul.3
CommitLineData
fea681da
MK
1.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
c13182ef 12.\"
fea681da
MK
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.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 23.\" %%%LICENSE_END
fea681da
MK
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.\"
9ba01802 33.TH STRTOUL 3 2019-03-06 "GNU" "Linux Programmer's Manual"
fea681da
MK
34.SH NAME
35strtoul, strtoull, strtouq \- convert a string to an unsigned long integer
36.SH SYNOPSIS
37.nf
38.B #include <stdlib.h>
68e4db0a 39.PP
62218dc0
MK
40.BI "unsigned long int strtoul(const char *" nptr ", char **" endptr \
41", int " base );
68e4db0a 42.PP
62218dc0
MK
43.BI "unsigned long long int strtoull(const char *" nptr ", char **" endptr ,
44.BI " int " base );
fea681da 45.fi
68e4db0a 46.PP
cc4615cc
MK
47.in -4n
48Feature Test Macro Requirements for glibc (see
49.BR feature_test_macros (7)):
50.in
68e4db0a 51.PP
cc4615cc
MK
52.ad l
53.BR strtoull ():
e0a98111 54.RS 4
2b1b0424
MK
55_ISOC99_SOURCE ||
56 || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
e0a98111
MK
57.RE
58.ad
fea681da 59.SH DESCRIPTION
60a90ecd
MK
60The
61.BR strtoul ()
62function converts the initial part of the string
46d8df8e
MK
63in
64.I nptr
65to an
d9a10d9d
MK
66.I "unsigned long int"
67value according to the
46d8df8e
MK
68given
69.IR base ,
70which must be between 2 and 36 inclusive, or be
fea681da
MK
71the special value 0.
72.PP
3f266a43 73The string may begin with an arbitrary amount of white space (as
fea681da
MK
74determined by
75.BR isspace (3))
f81fb444 76followed by a single optional \(aq+\(aq or \(aq\-\(aq
c13182ef 77sign.
46d8df8e
MK
78If
79.I base
80is zero or 16, the string may then include a
928410b4 81"0x" prefix, and the number will be read in base 16; otherwise, a
46d8df8e
MK
82zero
83.I base
84is taken as 10 (decimal) unless the next character
f81fb444 85is \(aq0\(aq, in which case it is taken as 8 (octal).
fea681da 86.PP
d9a10d9d
MK
87The remainder of the string is converted to an
88.I "unsigned long int"
89value in the obvious manner,
90stopping at the first character which is not a
c13182ef 91valid digit in the given base.
f81fb444 92(In bases above 10, the letter \(aqA\(aq in
efaef3da 93either uppercase or lowercase represents 10, \(aqB\(aq represents 11, and so
f81fb444 94forth, with \(aqZ\(aq representing 35.)
fea681da 95.PP
46d8df8e
MK
96If
97.I endptr
98is not NULL,
60a90ecd
MK
99.BR strtoul ()
100stores the address of the
46d8df8e
MK
101first invalid character in
102.IR *endptr .
c13182ef 103If there were no digits at
60a90ecd
MK
104all,
105.BR strtoul ()
46d8df8e
MK
106stores the original value of
107.I nptr
108in
109.I *endptr
110(and returns 0).
111In particular, if
112.I *nptr
d1a71985 113is not \(aq\e0\(aq but
46d8df8e 114.I **endptr
d1a71985 115is \(aq\e0\(aq on return, the entire string is valid.
fea681da
MK
116.PP
117The
63aa9df0 118.BR strtoull ()
fea681da 119function works just like the
63aa9df0 120.BR strtoul ()
d9a10d9d
MK
121function but returns an
122.I "unsigned long long int"
123value.
47297adb 124.SH RETURN VALUE
60a90ecd
MK
125The
126.BR strtoul ()
127function returns either the result of the conversion
fea681da 128or, if there was a leading minus sign, the negation of the result of the
c13182ef 129conversion represented as an unsigned value,
74cee559 130unless the original (nonnegated) value would overflow; in
60a90ecd
MK
131the latter case,
132.BR strtoul ()
a9b4ebbc
MK
133returns
134.B ULONG_MAX
46d8df8e
MK
135and sets
136.I errno
137to
a9b4ebbc 138.BR ERANGE .
fea681da 139Precisely the same holds for
63aa9df0 140.BR strtoull ()
a9b4ebbc
MK
141(with
142.B ULLONG_MAX
143instead of
144.BR ULONG_MAX ).
fea681da
MK
145.SH ERRORS
146.TP
147.B EINVAL
148(not in C99)
149The given
150.I base
151contains an unsupported value.
152.TP
153.B ERANGE
154The resulting value was out of range.
dd3568a1 155.PP
46d8df8e 156The implementation may also set
51700fd7 157.IR errno
46d8df8e
MK
158to
159.B EINVAL
160in case
fea681da 161no conversion was performed (no digits seen, and 0 returned).
232ea96e 162.SH ATTRIBUTES
ea7d8816
PH
163For an explanation of the terms used in this section, see
164.BR attributes (7).
165.TS
166allbox;
167lbw32 lb lb
168l l l.
169Interface Attribute Value
170T{
232ea96e
PH
171.BR strtoul (),
172.BR strtoull (),
232ea96e 173.BR strtouq ()
ea7d8816
PH
174T} Thread safety MT-Safe locale
175.TE
47297adb 176.SH CONFORMING TO
a1863f84
MK
177.BR strtoul ():
178POSIX.1-2001, POSIX.1-2008, C89, C99 SVr4.
847e0d88 179.PP
a1863f84
MK
180.BR strtoull ():
181POSIX.1-2001, POSIX.1-2008, C99.
fea681da 182.SH NOTES
c13182ef 183Since
fefe023e 184.BR strtoul ()
a9b4ebbc 185can legitimately return 0 or
9c60f8f4
MK
186.B ULONG_MAX
187.RB ( ULLONG_MAX
a9b4ebbc 188for
fefe023e
MK
189.BR strtoull ())
190on both success and failure, the calling program should set
191.I errno
c13182ef 192to 0 before the call,
fefe023e
MK
193and then determine if an error occurred by checking whether
194.I errno
c7094399 195has a nonzero value after the call.
847e0d88 196.PP
190c7abe 197In locales other than the "C" locale, other strings may be accepted.
fea681da
MK
198(For example, the thousands separator of the current locale may be
199supported.)
dd3568a1 200.PP
fea681da 201BSD also has
bdd915e2 202.PP
fea681da 203.in +4n
bdd915e2 204.EX
37370164 205.BI "u_quad_t strtouq(const char *" nptr ", char **" endptr ", int " base );
bdd915e2
MK
206.EE
207.in
208.PP
fea681da
MK
209with completely analogous definition.
210Depending on the wordsize of the current architecture, this
211may be equivalent to
63aa9df0 212.BR strtoull ()
fea681da 213or to
63aa9df0 214.BR strtoul ().
847e0d88 215.PP
190c7abe 216Negative values are considered valid input and are
d9a10d9d
MK
217silently converted to the equivalent
218.I "unsigned long int"
219value.
5a1cae6b
MK
220.SH EXAMPLE
221See the example on the
222.BR strtol (3)
c13182ef 223manual page;
5a1cae6b 224the use of the functions described in this manual page is similar.
47297adb 225.SH SEE ALSO
c5453914 226.BR a64l (3),
fea681da
MK
227.BR atof (3),
228.BR atoi (3),
229.BR atol (3),
230.BR strtod (3),
995eea22
JW
231.BR strtol (3),
232.BR strtoumax (3)