]>
Commit | Line | Data |
---|---|---|
fea681da MK |
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. | |
c13182ef | 11 | .\" |
fea681da MK |
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. | |
c13182ef | 19 | .\" |
fea681da MK |
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 | |
cc4615cc | 29 | .TH STRTOL 3 2007-07-26 "GNU" "Linux Programmer's Manual" |
fea681da MK |
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 | |
62218dc0 | 36 | .BI "long int strtol(const char *" nptr ", char **" endptr ", int " base ); |
fea681da | 37 | .sp |
62218dc0 MK |
38 | .BI "long long int strtoll(const char *" nptr ", char **" endptr \ |
39 | ", int " base ); | |
fea681da | 40 | .fi |
cc4615cc MK |
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 | XOPEN_SOURCE >= 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE; or | |
50 | .I cc\ -std=c99 | |
51 | .ad b | |
fea681da | 52 | .SH DESCRIPTION |
60a90ecd MK |
53 | The |
54 | .BR strtol () | |
55 | function converts the initial part of the string | |
fea681da MK |
56 | in \fInptr\fP to a long integer value according to the given \fIbase\fP, |
57 | which must be between 2 and 36 inclusive, or be the special value 0. | |
58 | .PP | |
3f266a43 | 59 | The string may begin with an arbitrary amount of white space (as |
fea681da MK |
60 | determined by |
61 | .BR isspace (3)) | |
928410b4 | 62 | followed by a single optional '+' or '\-' sign. |
c13182ef | 63 | If \fIbase\fP is zero or 16, the string may then include a |
928410b4 | 64 | "0x" prefix, and the number will be read in base 16; otherwise, a |
fea681da | 65 | zero \fIbase\fP is taken as 10 (decimal) unless the next character |
928410b4 | 66 | is '0', in which case it is taken as 8 (octal). |
fea681da | 67 | .PP |
c13182ef | 68 | The remainder of the string is converted to a |
fefe023e MK |
69 | .I long int |
70 | value | |
fea681da | 71 | in the obvious manner, stopping at the first character which is not a |
c13182ef | 72 | valid digit in the given base. |
928410b4 MK |
73 | (In bases above 10, the letter 'A' in |
74 | either upper or lower case represents 10, 'B' represents 11, and so | |
75 | forth, with 'Z' representing 35.) | |
fea681da | 76 | .PP |
60a90ecd MK |
77 | If \fIendptr\fP is not NULL, |
78 | .BR strtol () | |
79 | stores the address of the | |
c13182ef MK |
80 | first invalid character in \fI*endptr\fP. |
81 | If there were no digits at | |
60a90ecd MK |
82 | all, |
83 | .BR strtol () | |
84 | stores the original value of \fInptr\fP in | |
fea681da | 85 | \fI*endptr\fP (and returns 0). |
c401054d MK |
86 | In particular, if \fI*nptr\fP is not '\\0' but \fI**endptr\fP |
87 | is '\\0' on return, the entire string is valid. | |
fea681da MK |
88 | .PP |
89 | The | |
63aa9df0 | 90 | .BR strtoll () |
fea681da | 91 | function works just like the |
63aa9df0 | 92 | .BR strtol () |
fea681da MK |
93 | function but returns a long long integer value. |
94 | .SH "RETURN VALUE" | |
60a90ecd MK |
95 | The |
96 | .BR strtol () | |
97 | function returns the result of the conversion, | |
c13182ef MK |
98 | unless the value would underflow or overflow. |
99 | If an underflow occurs, | |
60a90ecd | 100 | .BR strtol () |
097585ed MK |
101 | returns |
102 | .BR LONG_MIN . | |
60a90ecd MK |
103 | If an overflow occurs, |
104 | .BR strtol () | |
097585ed MK |
105 | returns |
106 | .BR LONG_MAX . | |
09b235db MK |
107 | In both cases, \fIerrno\fP is set to |
108 | .BR ERANGE . | |
fea681da | 109 | Precisely the same holds for |
63aa9df0 | 110 | .BR strtoll () |
097585ed MK |
111 | (with |
112 | .B LLONG_MIN | |
113 | and | |
114 | .B LLONG_MAX | |
115 | instead of | |
116 | .B LONG_MIN | |
117 | and | |
118 | .BR LONG_MAX ). | |
fea681da MK |
119 | .SH ERRORS |
120 | .TP | |
121 | .B EINVAL | |
122 | (not in C99) | |
123 | The given | |
124 | .I base | |
125 | contains an unsupported value. | |
126 | .TP | |
127 | .B ERANGE | |
128 | The resulting value was out of range. | |
129 | .LP | |
130 | The implementation may also set \fIerrno\fP to \fBEINVAL\fP in case | |
131 | no conversion was performed (no digits seen, and 0 returned). | |
2b2581ee MK |
132 | .SH "CONFORMING TO" |
133 | .BR strtol () | |
134 | conforms to SVr4, 4.3BSD, C89, C99 and POSIX.1-2001, and | |
135 | .BR strtoll () | |
136 | to C99 and POSIX.1-2001. | |
fea681da | 137 | .SH NOTES |
c13182ef | 138 | Since |
fefe023e | 139 | .BR strtol () |
c13182ef | 140 | can legitimately return 0, |
097585ed MK |
141 | .BR LONG_MAX , |
142 | or | |
143 | .B LONG_MIN | |
144 | .RB ( LLONG_MAX | |
145 | or | |
146 | .B LLONG_MIN | |
147 | for | |
fefe023e MK |
148 | .BR strtoll ()) |
149 | on both success and failure, the calling program should set | |
150 | .I errno | |
c13182ef | 151 | to 0 before the call, |
fefe023e MK |
152 | and then determine if an error occurred by checking whether |
153 | .I errno | |
c382a365 | 154 | has a nonzero value after the call. |
fefe023e | 155 | |
f1158c07 | 156 | In locales other than the "C" locale, other strings may also be accepted. |
fea681da MK |
157 | (For example, the thousands separator of the current locale may be |
158 | supported.) | |
159 | .LP | |
160 | BSD also has | |
161 | .sp | |
162 | .in +4n | |
163 | .nf | |
a08ea57c | 164 | .BI "quad_t strtoq(const char *" nptr ", char **" endptr ", int " base ); |
fea681da | 165 | .sp |
a08ea57c | 166 | .in |
fea681da MK |
167 | .fi |
168 | with completely analogous definition. | |
169 | Depending on the wordsize of the current architecture, this | |
170 | may be equivalent to | |
63aa9df0 | 171 | .BR strtoll () |
fea681da | 172 | or to |
63aa9df0 | 173 | .BR strtol (). |
56aee868 MK |
174 | .SH EXAMPLE |
175 | The program shown below demonstrates the use of | |
176 | .BR strtol (). | |
c13182ef | 177 | The first command line argument specifies a string from which |
56aee868 MK |
178 | .BR strtol () |
179 | should parse a number. | |
c13182ef | 180 | The second (optional) argument specifies the base to be used for |
56aee868 | 181 | the conversion. |
0acd0e57 MK |
182 | (This argument is converted to numeric form using |
183 | .BR atoi (3), | |
c13182ef | 184 | a function that performs no error checking and |
0acd0e57 MK |
185 | has a simpler interface than |
186 | .BR strtol ().) | |
56aee868 | 187 | Some examples of the results produced by this program are the following: |
a08ea57c | 188 | .in +4n |
56aee868 MK |
189 | .nf |
190 | ||
191 | $ ./a.out 123 | |
192 | strtol() returned 123 | |
193 | $ ./a.out ' 123' | |
194 | strtol() returned 123 | |
195 | $ ./a.out 123abc | |
196 | strtol() returned 123 | |
197 | Further characters after number: abc | |
198 | $ ./a.out 123abc 55 | |
199 | strtol: Invalid argument | |
200 | $ ./a.out '' | |
201 | No digits were found | |
202 | $ ./a.out 4000000000 | |
203 | strtol: Numerical result out of range | |
204 | ||
205 | .fi | |
a08ea57c | 206 | .in |
56aee868 MK |
207 | The source code of the program is as follows: |
208 | .nf | |
209 | ||
210 | #include <stdlib.h> | |
211 | #include <limits.h> | |
212 | #include <stdio.h> | |
213 | #include <errno.h> | |
214 | ||
215 | int | |
216 | main(int argc, char *argv[]) | |
217 | { | |
218 | int base; | |
219 | char *endptr, *str; | |
220 | long val; | |
221 | ||
222 | if (argc < 2) { | |
223 | fprintf(stderr, "Usage: %s str [base]\\n", argv[0]); | |
224 | exit(EXIT_FAILURE); | |
c13182ef | 225 | } |
56aee868 MK |
226 | |
227 | str = argv[1]; | |
228 | base = (argc > 2) ? atoi(argv[2]) : 10; | |
229 | ||
230 | errno = 0; /* To distinguish success/failure after call */ | |
231 | val = strtol(str, &endptr, base); | |
232 | ||
233 | /* Check for various possible errors */ | |
234 | ||
235 | if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) | |
236 | || (errno != 0 && val == 0)) { | |
237 | perror("strtol"); | |
238 | exit(EXIT_FAILURE); | |
c13182ef | 239 | } |
56aee868 MK |
240 | |
241 | if (endptr == str) { | |
242 | fprintf(stderr, "No digits were found\\n"); | |
243 | exit(EXIT_FAILURE); | |
c13182ef | 244 | } |
56aee868 | 245 | |
0acd0e57 | 246 | /* If we got here, strtol() successfully parsed a number */ |
56aee868 MK |
247 | |
248 | printf("strtol() returned %ld\\n", val); | |
249 | ||
250 | if (*endptr != '\\0') /* Not necessarily an error... */ | |
251 | printf("Further characters after number: %s\\n", endptr); | |
252 | ||
253 | exit(EXIT_SUCCESS); | |
254 | } | |
255 | .fi | |
fea681da MK |
256 | .SH "SEE ALSO" |
257 | .BR atof (3), | |
258 | .BR atoi (3), | |
259 | .BR atol (3), | |
260 | .BR strtod (3), | |
261 | .BR strtoul (3) |