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

425 lines
9.9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of STRTOL</TITLE>
</HEAD><BODY>
<H1>STRTOL</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-10-10<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>
strtol, strtoll, strtoq - convert a string to a 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>long int strtol(const char *</B><I>nptr</I><B>, char **</B><I>endptr</I><B>, int </B><I>base</I><B>);</B>
<B>long long int strtoll(const char *</B><I>nptr</I><B>, char **</B><I>endptr</I><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>strtoll</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>strtol</B>()
function converts the initial part of the string
in
<I>nptr</I>
to a long integer 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; or &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 a
<I>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>strtol</B>()
stores the address of the
first invalid character in
<I>*endptr</I>.
If there were no digits at
all,
<B>strtol</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>strtoll</B>()
function works just like the
<B>strtol</B>()
function but returns a long long integer value.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
The
<B>strtol</B>()
function returns the result of the conversion,
unless the value would underflow or overflow.
If an underflow occurs,
<B>strtol</B>()
returns
<B>LONG_MIN</B>.
If an overflow occurs,
<B>strtol</B>()
returns
<B>LONG_MAX</B>.
In both cases,
<I>errno</I>
is set to
<B>ERANGE</B>.
Precisely the same holds for
<B>strtoll</B>()
(with
<B>LLONG_MIN</B>
and
<B>LLONG_MAX</B>
instead of
<B>LONG_MIN</B>
and
<B>LONG_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>strtol</B>(),
<B>strtoll</B>(),
<B>strtoq</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe locale<BR></TD></TR>
</TABLE>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>strtol</B>():
POSIX.1-2001, POSIX.1-2008, C89, C99 SVr4, 4.3BSD.
<P>
<B>strtoll</B>():
POSIX.1-2001, POSIX.1-2008, C99.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
Since
<B>strtol</B>()
can legitimately return 0,
<B>LONG_MAX</B>,
or
<B>LONG_MIN</B>
(<B>LLONG_MAX</B>
or
<B>LLONG_MIN</B>
for
<B>strtoll</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>
According to POSIX.1,
in locales other than the &quot;C&quot; and &quot;POSIX&quot;,
these functions may accept other,
implementation-defined numeric strings.
<P>
BSD also has
<P>
<B>quad_t strtoq(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>strtoll</B>()
or to
<B>strtol</B>().
<A NAME="lbAJ">&nbsp;</A>
<H2>EXAMPLE</H2>
The program shown below demonstrates the use of
<B>strtol</B>().
The first command-line argument specifies a string from which
<B>strtol</B>()
should parse a number.
The second (optional) argument specifies the base to be used for
the conversion.
(This argument is converted to numeric form using
<B><A HREF="/cgi-bin/man/man2html?3+atoi">atoi</A></B>(3),
a function that performs no error checking and
has a simpler interface than
<B>strtol</B>().)
Some examples of the results produced by this program are the following:
<P>
$<B> ./a.out 123</B>
strtol() returned 123
$<B> ./a.out ' 123'</B>
strtol() returned 123
$<B> ./a.out 123abc</B>
strtol() returned 123
Further characters after number: abc
$<B> ./a.out 123abc 55</B>
strtol: Invalid argument
$<B> ./a.out ''</B>
No digits were found
$<B> ./a.out 4000000000</B>
strtol: Numerical result out of range
<A NAME="lbAK">&nbsp;</A>
<H3>Program source</H3>
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/limits.h">limits.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/errno.h">errno.h</A>&gt;
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;base;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;*endptr,&nbsp;*str;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;val;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;&lt;&nbsp;2)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;str&nbsp;[base]\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;str&nbsp;=&nbsp;argv[1];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;base&nbsp;=&nbsp;(argc&nbsp;&gt;&nbsp;2)&nbsp;?&nbsp;atoi(argv[2])&nbsp;:&nbsp;10;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;errno&nbsp;=&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;To&nbsp;distinguish&nbsp;success/failure&nbsp;after&nbsp;call&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;val&nbsp;=&nbsp;strtol(str,&nbsp;&amp;endptr,&nbsp;base);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Check&nbsp;for&nbsp;various&nbsp;possible&nbsp;errors&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;((errno&nbsp;==&nbsp;ERANGE&nbsp;&amp;&amp;&nbsp;(val&nbsp;==&nbsp;LONG_MAX&nbsp;||&nbsp;val&nbsp;==&nbsp;LONG_MIN))
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;(errno&nbsp;!=&nbsp;0&nbsp;&amp;&amp;&nbsp;val&nbsp;==&nbsp;0))&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;strtol&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(endptr&nbsp;==&nbsp;str)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;No&nbsp;digits&nbsp;were&nbsp;found\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;If&nbsp;we&nbsp;got&nbsp;here,&nbsp;strtol()&nbsp;successfully&nbsp;parsed&nbsp;a&nbsp;number&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;strtol()&nbsp;returned&nbsp;%ld\n&quot;,&nbsp;val);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(*endptr&nbsp;!=&nbsp;'\0')&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Not&nbsp;necessarily&nbsp;an&nbsp;error...&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Further&nbsp;characters&nbsp;after&nbsp;number:&nbsp;%s\n&quot;,&nbsp;endptr);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<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+strtoimax">strtoimax</A></B>(3)
<B><A HREF="/cgi-bin/man/man2html?3+strtoul">strtoul</A></B>(3),
<A NAME="lbAM">&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>
<DL>
<DT id="13"><A HREF="#lbAK">Program source</A><DD>
</DL>
<DT id="14"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="15"><A HREF="#lbAM">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>