]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/strtol.3
Various pages: Add missing commas in SEE ALSO part II
[thirdparty/man-pages.git] / man3 / strtol.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:53:39 1993 by Rik Faith (faith@cs.unc.edu)
30.\" Added correction due to nsd@bbc.com (Nick Duffek) - aeb, 950610
867c9b34 31.TH STRTOL 3 2019-10-10 "GNU" "Linux Programmer's Manual"
fea681da
MK
32.SH NAME
33strtol, strtoll, strtoq \- convert a string to a long integer
34.SH SYNOPSIS
35.nf
36.B #include <stdlib.h>
68e4db0a 37.PP
62218dc0 38.BI "long int strtol(const char *" nptr ", char **" endptr ", int " base );
68e4db0a 39.PP
62218dc0
MK
40.BI "long long int strtoll(const char *" nptr ", char **" endptr \
41", int " base );
fea681da 42.fi
68e4db0a 43.PP
cc4615cc
MK
44.in -4n
45Feature Test Macro Requirements for glibc (see
46.BR feature_test_macros (7)):
47.in
68e4db0a 48.PP
cc4615cc
MK
49.ad l
50.BR strtoll ():
e0a98111 51.RS 4
2b1b0424
MK
52_ISOC99_SOURCE
53 || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
e0a98111
MK
54.RE
55.ad
fea681da 56.SH DESCRIPTION
60a90ecd
MK
57The
58.BR strtol ()
59function converts the initial part of the string
46d8df8e
MK
60in
61.I nptr
62to a long integer value according to the given
63.IR base ,
fea681da
MK
64which must be between 2 and 36 inclusive, or be the special value 0.
65.PP
3f266a43 66The string may begin with an arbitrary amount of white space (as
fea681da
MK
67determined by
68.BR isspace (3))
f81fb444 69followed by a single optional \(aq+\(aq or \(aq\-\(aq sign.
46d8df8e
MK
70If
71.I base
72is zero or 16, the string may then include a
e5b5658c 73"0x" or "0X" prefix, and the number will be read in base 16; otherwise, a
46d8df8e
MK
74zero
75.I base
76is taken as 10 (decimal) unless the next character
f81fb444 77is \(aq0\(aq, in which case it is taken as 8 (octal).
fea681da 78.PP
c13182ef 79The remainder of the string is converted to a
fefe023e
MK
80.I long int
81value
fea681da 82in the obvious manner, stopping at the first character which is not a
c13182ef 83valid digit in the given base.
f81fb444 84(In bases above 10, the letter \(aqA\(aq in
efaef3da 85either uppercase or lowercase represents 10, \(aqB\(aq represents 11, and so
f81fb444 86forth, with \(aqZ\(aq representing 35.)
fea681da 87.PP
46d8df8e
MK
88If
89.I endptr
90is not NULL,
60a90ecd
MK
91.BR strtol ()
92stores the address of the
46d8df8e
MK
93first invalid character in
94.IR *endptr .
c13182ef 95If there were no digits at
60a90ecd
MK
96all,
97.BR strtol ()
46d8df8e
MK
98stores the original value of
99.I nptr
100in
101.I *endptr
102(and returns 0).
103In particular, if
104.I *nptr
d1a71985 105is not \(aq\e0\(aq but
46d8df8e 106.I **endptr
d1a71985 107is \(aq\e0\(aq on return, the entire string is valid.
fea681da
MK
108.PP
109The
63aa9df0 110.BR strtoll ()
fea681da 111function works just like the
63aa9df0 112.BR strtol ()
fea681da 113function but returns a long long integer value.
47297adb 114.SH RETURN VALUE
60a90ecd
MK
115The
116.BR strtol ()
117function returns the result of the conversion,
c13182ef
MK
118unless the value would underflow or overflow.
119If an underflow occurs,
60a90ecd 120.BR strtol ()
097585ed
MK
121returns
122.BR LONG_MIN .
60a90ecd
MK
123If an overflow occurs,
124.BR strtol ()
097585ed
MK
125returns
126.BR LONG_MAX .
46d8df8e
MK
127In both cases,
128.I errno
129is set to
09b235db 130.BR ERANGE .
fea681da 131Precisely the same holds for
63aa9df0 132.BR strtoll ()
097585ed
MK
133(with
134.B LLONG_MIN
135and
136.B LLONG_MAX
137instead of
138.B LONG_MIN
139and
140.BR LONG_MAX ).
fea681da
MK
141.SH ERRORS
142.TP
143.B EINVAL
144(not in C99)
145The given
146.I base
147contains an unsupported value.
148.TP
149.B ERANGE
150The resulting value was out of range.
dd3568a1 151.PP
46d8df8e 152The implementation may also set
51700fd7 153.IR errno
46d8df8e
MK
154to
155.B EINVAL
156in case
fea681da 157no conversion was performed (no digits seen, and 0 returned).
ca74032e 158.SH ATTRIBUTES
de80a16a
PH
159For an explanation of the terms used in this section, see
160.BR attributes (7).
161.TS
162allbox;
163lbw29 lb lb
164l l l.
165Interface Attribute Value
166T{
ca74032e
PH
167.BR strtol (),
168.BR strtoll (),
ca74032e 169.BR strtoq ()
de80a16a
PH
170T} Thread safety MT-Safe locale
171.TE
47297adb 172.SH CONFORMING TO
c2fb338e
MK
173.BR strtol ():
174POSIX.1-2001, POSIX.1-2008, C89, C99 SVr4, 4.3BSD.
847e0d88 175.PP
c2fb338e
MK
176.BR strtoll ():
177POSIX.1-2001, POSIX.1-2008, C99.
fea681da 178.SH NOTES
c13182ef 179Since
fefe023e 180.BR strtol ()
c13182ef 181can legitimately return 0,
097585ed
MK
182.BR LONG_MAX ,
183or
184.B LONG_MIN
185.RB ( LLONG_MAX
186or
187.B LLONG_MIN
188for
fefe023e
MK
189.BR strtoll ())
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
c2fb338e 197According to POSIX.1,
36127c0e 198in locales other than the "C" and "POSIX",
6bdb0096
MK
199these functions may accept other,
200implementation-defined numeric strings.
dd3568a1 201.PP
fea681da 202BSD also has
51f5698d 203.PP
fea681da 204.in +4n
bdd915e2 205.EX
a08ea57c 206.BI "quad_t strtoq(const char *" nptr ", char **" endptr ", int " base );
b8302363 207.EE
e646a1ba 208.in
b9c93deb 209.PP
fea681da
MK
210with completely analogous definition.
211Depending on the wordsize of the current architecture, this
212may be equivalent to
63aa9df0 213.BR strtoll ()
fea681da 214or to
63aa9df0 215.BR strtol ().
56aee868
MK
216.SH EXAMPLE
217The program shown below demonstrates the use of
218.BR strtol ().
76c44d83 219The first command-line argument specifies a string from which
56aee868
MK
220.BR strtol ()
221should parse a number.
c13182ef 222The second (optional) argument specifies the base to be used for
56aee868 223the conversion.
0acd0e57
MK
224(This argument is converted to numeric form using
225.BR atoi (3),
c13182ef 226a function that performs no error checking and
0acd0e57
MK
227has a simpler interface than
228.BR strtol ().)
56aee868 229Some examples of the results produced by this program are the following:
e646a1ba 230.PP
a08ea57c 231.in +4n
e646a1ba 232.EX
b43a3b30 233.RB "$" " ./a.out 123"
56aee868 234strtol() returned 123
b43a3b30 235.RB "$" " ./a.out \(aq 123\(aq"
56aee868 236strtol() returned 123
b43a3b30 237.RB "$" " ./a.out 123abc"
56aee868
MK
238strtol() returned 123
239Further characters after number: abc
b43a3b30 240.RB "$" " ./a.out 123abc 55"
56aee868 241strtol: Invalid argument
b43a3b30 242.RB "$" " ./a.out \(aq\(aq"
56aee868 243No digits were found
b43a3b30 244.RB "$" " ./a.out 4000000000"
56aee868 245strtol: Numerical result out of range
b8302363 246.EE
a08ea57c 247.in
9c330504 248.SS Program source
d84d0300 249\&
e7d0bb47 250.EX
56aee868
MK
251#include <stdlib.h>
252#include <limits.h>
253#include <stdio.h>
254#include <errno.h>
255
256int
257main(int argc, char *argv[])
258{
259 int base;
260 char *endptr, *str;
261 long val;
262
263 if (argc < 2) {
d1a71985 264 fprintf(stderr, "Usage: %s str [base]\en", argv[0]);
56aee868 265 exit(EXIT_FAILURE);
c13182ef 266 }
56aee868
MK
267
268 str = argv[1];
269 base = (argc > 2) ? atoi(argv[2]) : 10;
270
271 errno = 0; /* To distinguish success/failure after call */
272 val = strtol(str, &endptr, base);
273
274 /* Check for various possible errors */
275
276 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
277 || (errno != 0 && val == 0)) {
278 perror("strtol");
279 exit(EXIT_FAILURE);
c13182ef 280 }
56aee868
MK
281
282 if (endptr == str) {
d1a71985 283 fprintf(stderr, "No digits were found\en");
56aee868 284 exit(EXIT_FAILURE);
c13182ef 285 }
56aee868 286
0acd0e57 287 /* If we got here, strtol() successfully parsed a number */
56aee868 288
d1a71985 289 printf("strtol() returned %ld\en", val);
56aee868 290
d1a71985
MK
291 if (*endptr != \(aq\e0\(aq) /* Not necessarily an error... */
292 printf("Further characters after number: %s\en", endptr);
56aee868
MK
293
294 exit(EXIT_SUCCESS);
295}
e7d0bb47 296.EE
47297adb 297.SH SEE ALSO
fea681da
MK
298.BR atof (3),
299.BR atoi (3),
300.BR atol (3),
301.BR strtod (3),
bba4bbbd 302.BR strtoimax (3),
27f942ad 303.BR strtoul (3),