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