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>
37 .BI "strtol(const char *" nptr ", char **" endptr ", int " base );
40 .BI "strtoll(const char *" nptr ", char **" endptr ", int " base );
44 Feature Test Macro Requirements for glibc (see
45 .BR feature_test_macros (7)):
50 XOPEN_SOURCE >= 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE; or
56 function converts the initial part of the string
57 in \fInptr\fP to a long integer value according to the given \fIbase\fP,
58 which must be between 2 and 36 inclusive, or be the special value 0.
60 The string may begin with an arbitrary amount of white space (as
63 followed by a single optional '+' or '\-' sign.
64 If \fIbase\fP is zero or 16, the string may then include a
65 "0x" prefix, and the number will be read in base 16; otherwise, a
66 zero \fIbase\fP is taken as 10 (decimal) unless the next character
67 is '0', in which case it is taken as 8 (octal).
69 The remainder of the string is converted to a
72 in the obvious manner, stopping at the first character which is not a
73 valid digit in the given base.
74 (In bases above 10, the letter 'A' in
75 either upper or lower case represents 10, 'B' represents 11, and so
76 forth, with 'Z' representing 35.)
78 If \fIendptr\fP is not NULL,
80 stores the address of the
81 first invalid character in \fI*endptr\fP.
82 If there were no digits at
85 stores the original value of \fInptr\fP in
86 \fI*endptr\fP (and returns 0).
87 In particular, if \fI*nptr\fP is not '\\0' but \fI**endptr\fP
88 is '\\0' on return, the entire string is valid.
92 function works just like the
94 function but returns a long long integer value.
98 function returns the result of the conversion,
99 unless the value would underflow or overflow.
100 If an underflow occurs,
104 If an overflow occurs,
108 In both cases, \fIerrno\fP is set to
110 Precisely the same holds for
126 contains an unsupported value.
129 The resulting value was out of range.
131 The implementation may also set \fIerrno\fP to \fBEINVAL\fP in case
132 no conversion was performed (no digits seen, and 0 returned).
135 conforms to SVr4, 4.3BSD, C89, C99 and POSIX.1-2001, and
137 to C99 and POSIX.1-2001.
141 can legitimately return 0,
150 on both success and failure, the calling program should set
152 to 0 before the call,
153 and then determine if an error occurred by checking whether
155 has a non-zero value after the call.
157 In locales other than the "C" locale, other strings may also be accepted.
158 (For example, the thousands separator of the current locale may be
166 .BI "strtoq(const char *" nptr ", char **" endptr ", int " base );
170 with completely analogous definition.
171 Depending on the wordsize of the current architecture, this
177 The program shown below demonstrates the use of
179 The first command line argument specifies a string from which
181 should parse a number.
182 The second (optional) argument specifies the base to be used for
184 (This argument is converted to numeric form using
186 a function that performs no error checking and
187 has a simpler interface than
189 Some examples of the results produced by this program are the following:
194 strtol() returned 123
196 strtol() returned 123
198 strtol() returned 123
199 Further characters after number: abc
201 strtol: Invalid argument
205 strtol: Numerical result out of range
209 The source code of the program is as follows:
218 main(int argc, char *argv[])
225 fprintf(stderr, "Usage: %s str [base]\\n", argv[0]);
230 base = (argc > 2) ? atoi(argv[2]) : 10;
232 errno = 0; /* To distinguish success/failure after call */
233 val = strtol(str, &endptr, base);
235 /* Check for various possible errors */
237 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
238 || (errno != 0 && val == 0)) {
244 fprintf(stderr, "No digits were found\\n");
248 /* If we got here, strtol() successfully parsed a number */
250 printf("strtol() returned %ld\\n", val);
252 if (*endptr != '\\0') /* Not necessarily an error... */
253 printf("Further characters after number: %s\\n", endptr);