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