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