]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/strtol.3
share/mk/: distcheck: Run 'check' after 'build'
[thirdparty/man-pages.git] / man3 / strtol.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
28 .\" 386BSD man pages
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 2019-03-06 "GNU" "Linux Programmer's Manual"
32 .SH NAME
33 strtol, strtoll, strtoq \- convert a string to a long integer
34 .SH SYNOPSIS
35 .nf
36 .B #include <stdlib.h>
37 .PP
38 .BI "long int strtol(const char *" nptr ", char **" endptr ", int " base );
39 .PP
40 .BI "long long int strtoll(const char *" nptr ", char **" endptr \
41 ", int " base );
42 .fi
43 .PP
44 .in -4n
45 Feature Test Macro Requirements for glibc (see
46 .BR feature_test_macros (7)):
47 .in
48 .PP
49 .ad l
50 .BR strtoll ():
51 .RS 4
52 _ISOC99_SOURCE
53 || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
54 .RE
55 .ad
56 .SH DESCRIPTION
57 The
58 .BR strtol ()
59 function converts the initial part of the string
60 in
61 .I nptr
62 to a long integer value according to the given
63 .IR base ,
64 which must be between 2 and 36 inclusive, or be the special value 0.
65 .PP
66 The string may begin with an arbitrary amount of white space (as
67 determined by
68 .BR isspace (3))
69 followed by a single optional \(aq+\(aq or \(aq\-\(aq sign.
70 If
71 .I base
72 is zero or 16, the string may then include a
73 "0x" or "0X" prefix, and the number will be read in base 16; otherwise, a
74 zero
75 .I base
76 is taken as 10 (decimal) unless the next character
77 is \(aq0\(aq, in which case it is taken as 8 (octal).
78 .PP
79 The remainder of the string is converted to a
80 .I long int
81 value
82 in the obvious manner, stopping at the first character which is not a
83 valid digit in the given base.
84 (In bases above 10, the letter \(aqA\(aq in
85 either uppercase or lowercase represents 10, \(aqB\(aq represents 11, and so
86 forth, with \(aqZ\(aq representing 35.)
87 .PP
88 If
89 .I endptr
90 is not NULL,
91 .BR strtol ()
92 stores the address of the
93 first invalid character in
94 .IR *endptr .
95 If there were no digits at
96 all,
97 .BR strtol ()
98 stores the original value of
99 .I nptr
100 in
101 .I *endptr
102 (and returns 0).
103 In particular, if
104 .I *nptr
105 is not \(aq\e0\(aq but
106 .I **endptr
107 is \(aq\e0\(aq on return, the entire string is valid.
108 .PP
109 The
110 .BR strtoll ()
111 function works just like the
112 .BR strtol ()
113 function but returns a long long integer value.
114 .SH RETURN VALUE
115 The
116 .BR strtol ()
117 function returns the result of the conversion,
118 unless the value would underflow or overflow.
119 If an underflow occurs,
120 .BR strtol ()
121 returns
122 .BR LONG_MIN .
123 If an overflow occurs,
124 .BR strtol ()
125 returns
126 .BR LONG_MAX .
127 In both cases,
128 .I errno
129 is set to
130 .BR ERANGE .
131 Precisely the same holds for
132 .BR strtoll ()
133 (with
134 .B LLONG_MIN
135 and
136 .B LLONG_MAX
137 instead of
138 .B LONG_MIN
139 and
140 .BR LONG_MAX ).
141 .SH ERRORS
142 .TP
143 .B EINVAL
144 (not in C99)
145 The given
146 .I base
147 contains an unsupported value.
148 .TP
149 .B ERANGE
150 The resulting value was out of range.
151 .PP
152 The implementation may also set
153 .IR errno
154 to
155 .B EINVAL
156 in case
157 no conversion was performed (no digits seen, and 0 returned).
158 .SH ATTRIBUTES
159 For an explanation of the terms used in this section, see
160 .BR attributes (7).
161 .TS
162 allbox;
163 lbw29 lb lb
164 l l l.
165 Interface Attribute Value
166 T{
167 .BR strtol (),
168 .BR strtoll (),
169 .BR strtoq ()
170 T} Thread safety MT-Safe locale
171 .TE
172 .SH CONFORMING TO
173 .BR strtol ():
174 POSIX.1-2001, POSIX.1-2008, C89, C99 SVr4, 4.3BSD.
175 .PP
176 .BR strtoll ():
177 POSIX.1-2001, POSIX.1-2008, C99.
178 .SH NOTES
179 Since
180 .BR strtol ()
181 can legitimately return 0,
182 .BR LONG_MAX ,
183 or
184 .B LONG_MIN
185 .RB ( LLONG_MAX
186 or
187 .B LLONG_MIN
188 for
189 .BR strtoll ())
190 on both success and failure, the calling program should set
191 .I errno
192 to 0 before the call,
193 and then determine if an error occurred by checking whether
194 .I errno
195 has a nonzero value after the call.
196 .PP
197 According to POSIX.1,
198 in locales other than the "C" and "POSIX",
199 these functions may accept other,
200 implementation-defined numeric strings.
201 .PP
202 BSD also has
203 .PP
204 .in +4n
205 .EX
206 .BI "quad_t strtoq(const char *" nptr ", char **" endptr ", int " base );
207 .EE
208 .in
209 .PP
210 with completely analogous definition.
211 Depending on the wordsize of the current architecture, this
212 may be equivalent to
213 .BR strtoll ()
214 or to
215 .BR strtol ().
216 .SH EXAMPLE
217 The program shown below demonstrates the use of
218 .BR strtol ().
219 The first command-line argument specifies a string from which
220 .BR strtol ()
221 should parse a number.
222 The second (optional) argument specifies the base to be used for
223 the conversion.
224 (This argument is converted to numeric form using
225 .BR atoi (3),
226 a function that performs no error checking and
227 has a simpler interface than
228 .BR strtol ().)
229 Some examples of the results produced by this program are the following:
230 .PP
231 .in +4n
232 .EX
233 .RB "$" " ./a.out 123"
234 strtol() returned 123
235 .RB "$" " ./a.out \(aq 123\(aq"
236 strtol() returned 123
237 .RB "$" " ./a.out 123abc"
238 strtol() returned 123
239 Further characters after number: abc
240 .RB "$" " ./a.out 123abc 55"
241 strtol: Invalid argument
242 .RB "$" " ./a.out \(aq\(aq"
243 No digits were found
244 .RB "$" " ./a.out 4000000000"
245 strtol: Numerical result out of range
246 .EE
247 .in
248 .SS Program source
249 \&
250 .EX
251 #include <stdlib.h>
252 #include <limits.h>
253 #include <stdio.h>
254 #include <errno.h>
255
256 int
257 main(int argc, char *argv[])
258 {
259 int base;
260 char *endptr, *str;
261 long val;
262
263 if (argc < 2) {
264 fprintf(stderr, "Usage: %s str [base]\en", argv[0]);
265 exit(EXIT_FAILURE);
266 }
267
268 str = argv[1];
269 base = (argc > 2) ? atoi(argv[2]) : 10;
270
271 errno = 0; /* To distinguish success/failure after call */
272 val = strtol(str, &endptr, base);
273
274 /* Check for various possible errors */
275
276 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
277 || (errno != 0 && val == 0)) {
278 perror("strtol");
279 exit(EXIT_FAILURE);
280 }
281
282 if (endptr == str) {
283 fprintf(stderr, "No digits were found\en");
284 exit(EXIT_FAILURE);
285 }
286
287 /* If we got here, strtol() successfully parsed a number */
288
289 printf("strtol() returned %ld\en", val);
290
291 if (*endptr != \(aq\e0\(aq) /* Not necessarily an error... */
292 printf("Further characters after number: %s\en", endptr);
293
294 exit(EXIT_SUCCESS);
295 }
296 .EE
297 .SH SEE ALSO
298 .BR atof (3),
299 .BR atoi (3),
300 .BR atol (3),
301 .BR strtod (3),
302 .BR strtoul (3),
303 .BR strtoimax (3)