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