]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/strtol.3
ffix
[thirdparty/man-pages.git] / man3 / strtol.3
CommitLineData
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
31strtol, 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
43The
44.BR strtol ()
45function converts the initial part of the string
fea681da
MK
46in \fInptr\fP to a long integer value according to the given \fIbase\fP,
47which must be between 2 and 36 inclusive, or be the special value 0.
48.PP
3f266a43 49The string may begin with an arbitrary amount of white space (as
fea681da
MK
50determined by
51.BR isspace (3))
928410b4 52followed by a single optional '+' or '\-' sign.
c13182ef 53If \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 55zero \fIbase\fP is taken as 10 (decimal) unless the next character
928410b4 56is '0', in which case it is taken as 8 (octal).
fea681da 57.PP
c13182ef 58The remainder of the string is converted to a
fefe023e
MK
59.I long int
60value
fea681da 61in the obvious manner, stopping at the first character which is not a
c13182ef 62valid digit in the given base.
928410b4
MK
63(In bases above 10, the letter 'A' in
64either upper or lower case represents 10, 'B' represents 11, and so
65forth, with 'Z' representing 35.)
fea681da 66.PP
60a90ecd
MK
67If \fIendptr\fP is not NULL,
68.BR strtol ()
69stores the address of the
c13182ef
MK
70first invalid character in \fI*endptr\fP.
71If there were no digits at
60a90ecd
MK
72all,
73.BR strtol ()
74stores the original value of \fInptr\fP in
fea681da 75\fI*endptr\fP (and returns 0).
c401054d
MK
76In particular, if \fI*nptr\fP is not '\\0' but \fI**endptr\fP
77is '\\0' on return, the entire string is valid.
fea681da
MK
78.PP
79The
63aa9df0 80.BR strtoll ()
fea681da 81function works just like the
63aa9df0 82.BR strtol ()
fea681da
MK
83function but returns a long long integer value.
84.SH "RETURN VALUE"
60a90ecd
MK
85The
86.BR strtol ()
87function returns the result of the conversion,
c13182ef
MK
88unless the value would underflow or overflow.
89If an underflow occurs,
60a90ecd 90.BR strtol ()
097585ed
MK
91returns
92.BR LONG_MIN .
60a90ecd
MK
93If an overflow occurs,
94.BR strtol ()
097585ed
MK
95returns
96.BR LONG_MAX .
09b235db
MK
97In both cases, \fIerrno\fP is set to
98.BR ERANGE .
fea681da 99Precisely the same holds for
63aa9df0 100.BR strtoll ()
097585ed
MK
101(with
102.B LLONG_MIN
103and
104.B LLONG_MAX
105instead of
106.B LONG_MIN
107and
108.BR LONG_MAX ).
fea681da
MK
109.SH ERRORS
110.TP
111.B EINVAL
112(not in C99)
113The given
114.I base
115contains an unsupported value.
116.TP
117.B ERANGE
118The resulting value was out of range.
119.LP
120The implementation may also set \fIerrno\fP to \fBEINVAL\fP in case
121no conversion was performed (no digits seen, and 0 returned).
2b2581ee
MK
122.SH "CONFORMING TO"
123.BR strtol ()
124conforms to SVr4, 4.3BSD, C89, C99 and POSIX.1-2001, and
125.BR strtoll ()
126to C99 and POSIX.1-2001.
fea681da 127.SH NOTES
c13182ef 128Since
fefe023e 129.BR strtol ()
c13182ef 130can legitimately return 0,
097585ed
MK
131.BR LONG_MAX ,
132or
133.B LONG_MIN
134.RB ( LLONG_MAX
135or
136.B LLONG_MIN
137for
fefe023e
MK
138.BR strtoll ())
139on both success and failure, the calling program should set
140.I errno
c13182ef 141to 0 before the call,
fefe023e
MK
142and then determine if an error occurred by checking whether
143.I errno
144has a non-zero value after the call.
145
f1158c07 146In 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
148supported.)
149.LP
150BSD 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
159with completely analogous definition.
160Depending on the wordsize of the current architecture, this
161may be equivalent to
63aa9df0 162.BR strtoll ()
fea681da 163or to
63aa9df0 164.BR strtol ().
56aee868
MK
165.SH EXAMPLE
166The program shown below demonstrates the use of
167.BR strtol ().
c13182ef 168The first command line argument specifies a string from which
56aee868
MK
169.BR strtol ()
170should parse a number.
c13182ef 171The second (optional) argument specifies the base to be used for
56aee868 172the conversion.
0acd0e57
MK
173(This argument is converted to numeric form using
174.BR atoi (3),
c13182ef 175a function that performs no error checking and
0acd0e57
MK
176has a simpler interface than
177.BR strtol ().)
56aee868
MK
178Some examples of the results produced by this program are the following:
179.in +0.25i
180.nf
181
182$ ./a.out 123
183strtol() returned 123
184$ ./a.out ' 123'
185strtol() returned 123
186$ ./a.out 123abc
187strtol() returned 123
188Further characters after number: abc
189$ ./a.out 123abc 55
190strtol: Invalid argument
191$ ./a.out ''
192No digits were found
193$ ./a.out 4000000000
194strtol: Numerical result out of range
195
196.fi
197.in -0.25i
198The 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
206int
207main(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)