1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
27 .\" Modified Sun Jul 25 10:53:39 1993 by Rik Faith (faith@cs.unc.edu)
28 .\" Added correction due to nsd@bbc.com (Nick Duffek) - aeb, 950610
29 .TH STRTOL 3 2007-07-26 "GNU" "Linux Programmer's Manual"
31 strtol, strtoll, strtoq \- convert a string to a long integer
34 .B #include <stdlib.h>
36 .BI "long int strtol(const char *" nptr ", char **" endptr ", int " base );
38 .BI "long long int strtoll(const char *" nptr ", char **" endptr \
43 Feature Test Macro Requirements for glibc (see
44 .BR feature_test_macros (7)):
49 XOPEN_SOURCE >= 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE; or
55 function converts the initial part of the string
56 in \fInptr\fP to a long integer value according to the given \fIbase\fP,
57 which must be between 2 and 36 inclusive, or be the special value 0.
59 The string may begin with an arbitrary amount of white space (as
62 followed by a single optional '+' or '\-' sign.
63 If \fIbase\fP is zero or 16, the string may then include a
64 "0x" prefix, and the number will be read in base 16; otherwise, a
65 zero \fIbase\fP is taken as 10 (decimal) unless the next character
66 is '0', in which case it is taken as 8 (octal).
68 The remainder of the string is converted to a
71 in the obvious manner, stopping at the first character which is not a
72 valid digit in the given base.
73 (In bases above 10, the letter 'A' in
74 either upper or lower case represents 10, 'B' represents 11, and so
75 forth, with 'Z' representing 35.)
77 If \fIendptr\fP is not NULL,
79 stores the address of the
80 first invalid character in \fI*endptr\fP.
81 If there were no digits at
84 stores the original value of \fInptr\fP in
85 \fI*endptr\fP (and returns 0).
86 In particular, if \fI*nptr\fP is not '\\0' but \fI**endptr\fP
87 is '\\0' on return, the entire string is valid.
91 function works just like the
93 function but returns a long long integer value.
97 function returns the result of the conversion,
98 unless the value would underflow or overflow.
99 If an underflow occurs,
103 If an overflow occurs,
107 In both cases, \fIerrno\fP is set to
109 Precisely the same holds for
125 contains an unsupported value.
128 The resulting value was out of range.
130 The implementation may also set \fIerrno\fP to \fBEINVAL\fP in case
131 no conversion was performed (no digits seen, and 0 returned).
134 conforms to SVr4, 4.3BSD, C89, C99 and POSIX.1-2001, and
136 to C99 and POSIX.1-2001.
140 can legitimately return 0,
149 on both success and failure, the calling program should set
151 to 0 before the call,
152 and then determine if an error occurred by checking whether
154 has a nonzero value after the call.
156 In locales other than the "C" locale, other strings may also be accepted.
157 (For example, the thousands separator of the current locale may be
164 .BI "quad_t strtoq(const char *" nptr ", char **" endptr ", int " base );
168 with completely analogous definition.
169 Depending on the wordsize of the current architecture, this
175 The program shown below demonstrates the use of
177 The first command-line argument specifies a string from which
179 should parse a number.
180 The second (optional) argument specifies the base to be used for
182 (This argument is converted to numeric form using
184 a function that performs no error checking and
185 has a simpler interface than
187 Some examples of the results produced by this program are the following:
192 strtol() returned 123
194 strtol() returned 123
196 strtol() returned 123
197 Further characters after number: abc
199 strtol: Invalid argument
203 strtol: Numerical result out of range
207 The source code of the program is as follows:
216 main(int argc, char *argv[])
223 fprintf(stderr, "Usage: %s str [base]\\n", argv[0]);
228 base = (argc > 2) ? atoi(argv[2]) : 10;
230 errno = 0; /* To distinguish success/failure after call */
231 val = strtol(str, &endptr, base);
233 /* Check for various possible errors */
235 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
236 || (errno != 0 && val == 0)) {
242 fprintf(stderr, "No digits were found\\n");
246 /* If we got here, strtol() successfully parsed a number */
248 printf("strtol() returned %ld\\n", val);
250 if (*endptr != '\\0') /* Not necessarily an error... */
251 printf("Further characters after number: %s\\n", endptr);