1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\" and Copyright 2006 Michael Kerrisk <mtk.manpages@ganil.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" References consulted:
27 .\" Linux libc source code
28 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
30 .\" Modified Sun Jul 25 10:53:39 1993 by Rik Faith (faith@cs.unc.edu)
31 .\" Added correction due to nsd@bbc.com (Nick Duffek) - aeb, 950610
32 .TH STRTOL 3 2021-03-22 "GNU" "Linux Programmer's Manual"
34 strtol, strtoll, strtoq \- convert a string to a long integer
37 .B #include <stdlib.h>
39 .BI "long strtol(const char *restrict " nptr ,
40 .BI " char **restrict " endptr ", int " base );
41 .BI "long long strtoll(const char *restrict " nptr ,
42 .BI " char **restrict " endptr ", int " base );
46 Feature Test Macro Requirements for glibc (see
47 .BR feature_test_macros (7)):
53 || /* Glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
58 function converts the initial part of the string
61 to a long integer value according to the given
63 which must be between 2 and 36 inclusive, or be the special value 0.
65 The string may begin with an arbitrary amount of white space (as
68 followed by a single optional \(aq+\(aq or \(aq\-\(aq sign.
71 is zero or 16, the string may then include a
72 "0x" or "0X" prefix, and the number will be read in base 16; otherwise, a
75 is taken as 10 (decimal) unless the next character
76 is \(aq0\(aq, in which case it is taken as 8 (octal).
78 The remainder of the string is converted to a
81 in the obvious manner, stopping at the first character which is not a
82 valid digit in the given base.
83 (In bases above 10, the letter \(aqA\(aq in
84 either uppercase or lowercase represents 10, \(aqB\(aq represents 11, and so
85 forth, with \(aqZ\(aq representing 35.)
91 stores the address of the
92 first invalid character in
94 If there were no digits at
97 stores the original value of
104 is not \(aq\e0\(aq but
106 is \(aq\e0\(aq on return, the entire string is valid.
110 function works just like the
112 function but returns a
118 function returns the result of the conversion,
119 unless the value would underflow or overflow.
120 If an underflow occurs,
124 If an overflow occurs,
132 Precisely the same holds for
148 contains an unsupported value.
151 The resulting value was out of range.
153 The implementation may also set
158 no conversion was performed (no digits seen, and 0 returned).
160 For an explanation of the terms used in this section, see
168 Interface Attribute Value
173 T} Thread safety MT-Safe locale
180 POSIX.1-2001, POSIX.1-2008, C89, C99 SVr4, 4.3BSD.
183 POSIX.1-2001, POSIX.1-2008, C99.
187 can legitimately return 0,
196 on both success and failure, the calling program should set
198 to 0 before the call,
199 and then determine if an error occurred by checking whether
201 has a nonzero value after the call.
203 According to POSIX.1,
204 in locales other than "C" and "POSIX",
205 these functions may accept other,
206 implementation-defined numeric strings.
212 .BI "quad_t strtoq(const char *" nptr ", char **" endptr ", int " base );
216 with completely analogous definition.
217 Depending on the wordsize of the current architecture, this
223 The program shown below demonstrates the use of
225 The first command-line argument specifies a string from which
227 should parse a number.
228 The second (optional) argument specifies the base to be used for
230 (This argument is converted to numeric form using
232 a function that performs no error checking and
233 has a simpler interface than
235 Some examples of the results produced by this program are the following:
239 .RB "$" " ./a.out 123"
240 strtol() returned 123
241 .RB "$" " ./a.out \(aq 123\(aq"
242 strtol() returned 123
243 .RB "$" " ./a.out 123abc"
244 strtol() returned 123
245 Further characters after number: "abc"
246 .RB "$" " ./a.out 123abc 55"
247 strtol: Invalid argument
248 .RB "$" " ./a.out \(aq\(aq"
250 .RB "$" " ./a.out 4000000000"
251 strtol: Numerical result out of range
263 main(int argc, char *argv[])
270 fprintf(stderr, "Usage: %s str [base]\en", argv[0]);
275 base = (argc > 2) ? atoi(argv[2]) : 0;
277 errno = 0; /* To distinguish success/failure after call */
278 val = strtol(str, &endptr, base);
280 /* Check for various possible errors. */
288 fprintf(stderr, "No digits were found\en");
292 /* If we got here, strtol() successfully parsed a number. */
294 printf("strtol() returned %ld\en", val);
296 if (*endptr != \(aq\e0\(aq) /* Not necessarily an error... */
297 printf("Further characters after number: \e"%s\e"\en", endptr);