1STRTOL(3) Linux Programmer's Manual STRTOL(3)
2
3
4
6 strtol, strtoll, strtoq - convert a string to a long integer
7
9 #include <stdlib.h>
10
11 long int strtol(const char *nptr, char **endptr, int base);
12
13 long long int strtoll(const char *nptr, char **endptr, int base);
14
15 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17 strtoll(): XOPEN_SOURCE >= 600 || _BSD_SOURCE || _SVID_SOURCE ||
18 _ISOC99_SOURCE; or cc -std=c99
19
21 The strtol() function converts the initial part of the string in nptr
22 to a long integer value according to the given base, which must be
23 between 2 and 36 inclusive, or be the special value 0.
24
25 The string may begin with an arbitrary amount of white space (as deter‐
26 mined by isspace(3)) followed by a single optional '+' or '-' sign. If
27 base is zero or 16, the string may then include a "0x" prefix, and the
28 number will be read in base 16; otherwise, a zero base is taken as 10
29 (decimal) unless the next character is '0', in which case it is taken
30 as 8 (octal).
31
32 The remainder of the string is converted to a long int value in the
33 obvious manner, stopping at the first character which is not a valid
34 digit in the given base. (In bases above 10, the letter 'A' in either
35 upper or lower case represents 10, 'B' represents 11, and so forth,
36 with 'Z' representing 35.)
37
38 If endptr is not NULL, strtol() stores the address of the first invalid
39 character in *endptr. If there were no digits at all, strtol() stores
40 the original value of nptr in *endptr (and returns 0). In particular,
41 if *nptr is not '\0' but **endptr is '\0' on return, the entire string
42 is valid.
43
44 The strtoll() function works just like the strtol() function but
45 returns a long long integer value.
46
48 The strtol() function returns the result of the conversion, unless the
49 value would underflow or overflow. If an underflow occurs, strtol()
50 returns LONG_MIN. If an overflow occurs, strtol() returns LONG_MAX.
51 In both cases, errno is set to ERANGE. Precisely the same holds for
52 strtoll() (with LLONG_MIN and LLONG_MAX instead of LONG_MIN and
53 LONG_MAX).
54
56 EINVAL (not in C99) The given base contains an unsupported value.
57
58 ERANGE The resulting value was out of range.
59
60 The implementation may also set errno to EINVAL in case no conversion
61 was performed (no digits seen, and 0 returned).
62
64 strtol() conforms to SVr4, 4.3BSD, C89, C99 and POSIX.1-2001, and str‐
65 toll() to C99 and POSIX.1-2001.
66
68 Since strtol() can legitimately return 0, LONG_MAX, or LONG_MIN
69 (LLONG_MAX or LLONG_MIN for strtoll()) on both success and failure, the
70 calling program should set errno to 0 before the call, and then deter‐
71 mine if an error occurred by checking whether errno has a nonzero value
72 after the call.
73
74 In locales other than the "C" locale, other strings may also be
75 accepted. (For example, the thousands separator of the current locale
76 may be supported.)
77
78 BSD also has
79
80 quad_t strtoq(const char *nptr, char **endptr, int base);
81
82 with completely analogous definition. Depending on the wordsize of the
83 current architecture, this may be equivalent to strtoll() or to str‐
84 tol().
85
87 The program shown below demonstrates the use of strtol(). The first
88 command-line argument specifies a string from which strtol() should
89 parse a number. The second (optional) argument specifies the base to
90 be used for the conversion. (This argument is converted to numeric
91 form using atoi(3), a function that performs no error checking and has
92 a simpler interface than strtol().) Some examples of the results pro‐
93 duced by this program are the following:
94
95 $ ./a.out 123
96 strtol() returned 123
97 $ ./a.out ' 123'
98 strtol() returned 123
99 $ ./a.out 123abc
100 strtol() returned 123
101 Further characters after number: abc
102 $ ./a.out 123abc 55
103 strtol: Invalid argument
104 $ ./a.out ''
105 No digits were found
106 $ ./a.out 4000000000
107 strtol: Numerical result out of range
108
109 Program source
110
111 #include <stdlib.h>
112 #include <limits.h>
113 #include <stdio.h>
114 #include <errno.h>
115
116 int
117 main(int argc, char *argv[])
118 {
119 int base;
120 char *endptr, *str;
121 long val;
122
123 if (argc < 2) {
124 fprintf(stderr, "Usage: %s str [base]\n", argv[0]);
125 exit(EXIT_FAILURE);
126 }
127
128 str = argv[1];
129 base = (argc > 2) ? atoi(argv[2]) : 10;
130
131 errno = 0; /* To distinguish success/failure after call */
132 val = strtol(str, &endptr, base);
133
134 /* Check for various possible errors */
135
136 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
137 || (errno != 0 && val == 0)) {
138 perror("strtol");
139 exit(EXIT_FAILURE);
140 }
141
142 if (endptr == str) {
143 fprintf(stderr, "No digits were found\n");
144 exit(EXIT_FAILURE);
145 }
146
147 /* If we got here, strtol() successfully parsed a number */
148
149 printf("strtol() returned %ld\n", val);
150
151 if (*endptr != '\0') /* Not necessarily an error... */
152 printf("Further characters after number: %s\n", endptr);
153
154 exit(EXIT_SUCCESS);
155 }
156
158 atof(3), atoi(3), atol(3), strtod(3), strtoul(3)
159
161 This page is part of release 3.25 of the Linux man-pages project. A
162 description of the project, and information about reporting bugs, can
163 be found at http://www.kernel.org/doc/man-pages/.
164
165
166
167GNU 2007-07-26 STRTOL(3)