1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" %%%LICENSE_START(VERBATIM)
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
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
31 .TH STRTOL 3 2013-02-10 "GNU" "Linux Programmer's Manual"
33 strtol, strtoll, strtoq \- convert a string to a long integer
36 .B #include <stdlib.h>
38 .BI "long int strtol(const char *" nptr ", char **" endptr ", int " base );
40 .BI "long long int strtoll(const char *" nptr ", char **" endptr \
45 Feature Test Macro Requirements for glibc (see
46 .BR feature_test_macros (7)):
52 XOPEN_SOURCE\ >=\ 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE ||
53 _POSIX_C_SOURCE\ >=\ 200112L;
62 function converts the initial part of the string
63 in \fInptr\fP to a long integer value according to the given \fIbase\fP,
64 which must be between 2 and 36 inclusive, or be the special value 0.
66 The string may begin with an arbitrary amount of white space (as
69 followed by a single optional \(aq+\(aq or \(aq\-\(aq sign.
70 If \fIbase\fP is zero or 16, the string may then include a
71 "0x" prefix, and the number will be read in base 16; otherwise, a
72 zero \fIbase\fP is taken as 10 (decimal) unless the next character
73 is \(aq0\(aq, in which case it is taken as 8 (octal).
75 The remainder of the string is converted to a
78 in the obvious manner, stopping at the first character which is not a
79 valid digit in the given base.
80 (In bases above 10, the letter \(aqA\(aq in
81 either upper or lower case represents 10, \(aqB\(aq represents 11, and so
82 forth, with \(aqZ\(aq representing 35.)
84 If \fIendptr\fP is not NULL,
86 stores the address of the
87 first invalid character in \fI*endptr\fP.
88 If there were no digits at
91 stores the original value of \fInptr\fP in
92 \fI*endptr\fP (and returns 0).
93 In particular, if \fI*nptr\fP is not \(aq\\0\(aq but \fI**endptr\fP
94 is \(aq\\0\(aq on return, the entire string is valid.
98 function works just like the
100 function but returns a long long integer value.
104 function returns the result of the conversion,
105 unless the value would underflow or overflow.
106 If an underflow occurs,
110 If an overflow occurs,
114 In both cases, \fIerrno\fP is set to
116 Precisely the same holds for
132 contains an unsupported value.
135 The resulting value was out of range.
137 The implementation may also set \fIerrno\fP to \fBEINVAL\fP in case
138 no conversion was performed (no digits seen, and 0 returned).
141 conforms to SVr4, 4.3BSD, C89, C99 and POSIX.1-2001, and
143 to C99 and POSIX.1-2001.
147 can legitimately return 0,
156 on both success and failure, the calling program should set
158 to 0 before the call,
159 and then determine if an error occurred by checking whether
161 has a nonzero value after the call.
163 According to POSIX.1-2001,
164 in locales other than the "C" and "POSIX",
165 these functions may accept other,
166 implementation-defined numeric strings.
172 .BI "quad_t strtoq(const char *" nptr ", char **" endptr ", int " base );
176 with completely analogous definition.
177 Depending on the wordsize of the current architecture, this
183 The program shown below demonstrates the use of
185 The first command-line argument specifies a string from which
187 should parse a number.
188 The second (optional) argument specifies the base to be used for
190 (This argument is converted to numeric form using
192 a function that performs no error checking and
193 has a simpler interface than
195 Some examples of the results produced by this program are the following:
199 .RB "$" " ./a.out 123"
200 strtol() returned 123
201 .RB "$" " ./a.out \(aq 123\(aq"
202 strtol() returned 123
203 .RB "$" " ./a.out 123abc"
204 strtol() returned 123
205 Further characters after number: abc
206 .RB "$" " ./a.out 123abc 55"
207 strtol: Invalid argument
208 .RB "$" " ./a.out \(aq\(aq"
210 .RB "$" " ./a.out 4000000000"
211 strtol: Numerical result out of range
223 main(int argc, char *argv[])
230 fprintf(stderr, "Usage: %s str [base]\\n", argv[0]);
235 base = (argc > 2) ? atoi(argv[2]) : 10;
237 errno = 0; /* To distinguish success/failure after call */
238 val = strtol(str, &endptr, base);
240 /* Check for various possible errors */
242 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
243 || (errno != 0 && val == 0)) {
249 fprintf(stderr, "No digits were found\\n");
253 /* If we got here, strtol() successfully parsed a number */
255 printf("strtol() returned %ld\\n", val);
257 if (*endptr != \(aq\\0\(aq) /* Not necessarily an error... */
258 printf("Further characters after number: %s\\n", endptr);