man-pages/man3/strtoul.3.html
2021-03-31 01:06:50 +01:00

336 lines
6.8 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of STRTOUL</TITLE>
</HEAD><BODY>
<H1>STRTOUL</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
strtoul, strtoull, strtouq - convert a string to an unsigned long integer
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;</B>
<B>unsigned long int strtoul(const char *</B><I>nptr</I><B>, char **</B><I>endptr</I><B>, int </B><I>base</I><B>);</B>
<B>unsigned long long int strtoull(const char *</B><I>nptr</I><B>, char **</B><I>endptr</I><B>,</B>
<B> int </B><I>base</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>strtoull</B>():
<DL COMPACT><DT id="1"><DD>
_ISOC99_SOURCE ||
<BR>&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;/*&nbsp;Glibc&nbsp;versions&nbsp;&lt;=&nbsp;2.19:&nbsp;*/&nbsp;_SVID_SOURCE&nbsp;||&nbsp;_BSD_SOURCE
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>strtoul</B>()
function converts the initial part of the string
in
<I>nptr</I>
to an
<I>unsigned long int</I>
value according to the
given
<I>base</I>,
which must be between 2 and 36 inclusive, or be
the special value 0.
<P>
The string may begin with an arbitrary amount of white space (as
determined by
<B><A HREF="/cgi-bin/man/man2html?3+isspace">isspace</A></B>(3))
followed by a single optional '+' or '-'
sign.
If
<I>base</I>
is zero or 16, the string may then include a
&quot;0x&quot; prefix, and the number will be read in base 16; otherwise, a
zero
<I>base</I>
is taken as 10 (decimal) unless the next character
is '0', in which case it is taken as 8 (octal).
<P>
The remainder of the string is converted to an
<I>unsigned long int</I>
value in the obvious manner,
stopping at the first character which is not a
valid digit in the given base.
(In bases above 10, the letter 'A' in
either uppercase or lowercase represents 10, 'B' represents 11, and so
forth, with 'Z' representing 35.)
<P>
If
<I>endptr</I>
is not NULL,
<B>strtoul</B>()
stores the address of the
first invalid character in
<I>*endptr</I>.
If there were no digits at
all,
<B>strtoul</B>()
stores the original value of
<I>nptr</I>
in
<I>*endptr</I>
(and returns 0).
In particular, if
<I>*nptr</I>
is not '\0' but
<I>**endptr</I>
is '\0' on return, the entire string is valid.
<P>
The
<B>strtoull</B>()
function works just like the
<B>strtoul</B>()
function but returns an
<I>unsigned long long int</I>
value.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
The
<B>strtoul</B>()
function returns either the result of the conversion
or, if there was a leading minus sign, the negation of the result of the
conversion represented as an unsigned value,
unless the original (nonnegated) value would overflow; in
the latter case,
<B>strtoul</B>()
returns
<B>ULONG_MAX</B>
and sets
<I>errno</I>
to
<B>ERANGE</B>.
Precisely the same holds for
<B>strtoull</B>()
(with
<B>ULLONG_MAX</B>
instead of
<B>ULONG_MAX</B>).
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="2"><B>EINVAL</B>
<DD>
(not in C99)
The given
<I>base</I>
contains an unsupported value.
<DT id="3"><B>ERANGE</B>
<DD>
The resulting value was out of range.
</DL>
<P>
The implementation may also set
<I>errno</I>
to
<B>EINVAL</B>
in case
no conversion was performed (no digits seen, and 0 returned).
<A NAME="lbAG">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>strtoul</B>(),
<B>strtoull</B>(),
<B>strtouq</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe locale<BR></TD></TR>
</TABLE>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>strtoul</B>():
POSIX.1-2001, POSIX.1-2008, C89, C99 SVr4.
<P>
<B>strtoull</B>():
POSIX.1-2001, POSIX.1-2008, C99.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
Since
<B>strtoul</B>()
can legitimately return 0 or
<B>ULONG_MAX</B>
(<B>ULLONG_MAX</B>
for
<B>strtoull</B>())
on both success and failure, the calling program should set
<I>errno</I>
to 0 before the call,
and then determine if an error occurred by checking whether
<I>errno</I>
has a nonzero value after the call.
<P>
In locales other than the &quot;C&quot; locale, other strings may be accepted.
(For example, the thousands separator of the current locale may be
supported.)
<P>
BSD also has
<P>
<B>u_quad_t strtouq(const char *</B><I>nptr</I><B>, char **</B><I>endptr</I><B>, int </B><I>base</I><B>);</B>
<P>
with completely analogous definition.
Depending on the wordsize of the current architecture, this
may be equivalent to
<B>strtoull</B>()
or to
<B>strtoul</B>().
<P>
Negative values are considered valid input and are
silently converted to the equivalent
<I>unsigned long int</I>
value.
<A NAME="lbAJ">&nbsp;</A>
<H2>EXAMPLE</H2>
See the example on the
<B><A HREF="/cgi-bin/man/man2html?3+strtol">strtol</A></B>(3)
manual page;
the use of the functions described in this manual page is similar.
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+a64l">a64l</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+atof">atof</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+atoi">atoi</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+atol">atol</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+strtod">strtod</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+strtol">strtol</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+strtoumax">strtoumax</A></B>(3)
<A NAME="lbAL">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="4"><A HREF="#lbAB">NAME</A><DD>
<DT id="5"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="6"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="7"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="8"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="9"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="10"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="11"><A HREF="#lbAI">NOTES</A><DD>
<DT id="12"><A HREF="#lbAJ">EXAMPLE</A><DD>
<DT id="13"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="14"><A HREF="#lbAL">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:58 GMT, March 31, 2021
</BODY>
</HTML>