1STRTOL(3)                  Linux Programmer's Manual                 STRTOL(3)
2
3
4

NAME

6       strtol, strtoll, strtoq - convert a string to a long integer
7

SYNOPSIS

9       #include <stdlib.h>
10
11       long int strtol(const char *nptr, char **endptr, int base);
12
13       long long int strtoll(const char *nptr, char **endptr, int base);
14
15   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17       strtoll():
18           XOPEN_SOURCE >= 600 || _BSD_SOURCE || _SVID_SOURCE ||
19           _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L;
20           or cc -std=c99
21

DESCRIPTION

23       The strtol() function converts the initial part of the string  in  nptr
24       to  a  long  integer  value  according to the given base, which must be
25       between 2 and 36 inclusive, or be the special value 0.
26
27       The string may begin with an arbitrary amount of white space (as deter‐
28       mined by isspace(3)) followed by a single optional '+' or '-' sign.  If
29       base is zero or 16, the string may then include a "0x" prefix, and  the
30       number  will  be read in base 16; otherwise, a zero base is taken as 10
31       (decimal) unless the next character is '0', in which case it  is  taken
32       as 8 (octal).
33
34       The  remainder  of  the  string is converted to a long int value in the
35       obvious manner, stopping at the first character which is  not  a  valid
36       digit  in the given base.  (In bases above 10, the letter 'A' in either
37       upper or lower case represents 10, 'B' represents  11,  and  so  forth,
38       with 'Z' representing 35.)
39
40       If endptr is not NULL, strtol() stores the address of the first invalid
41       character in *endptr.  If there were no digits at all, strtol()  stores
42       the  original value of nptr in *endptr (and returns 0).  In particular,
43       if *nptr is not '\0' but **endptr is '\0' on return, the entire  string
44       is valid.
45
46       The  strtoll()  function  works  just  like  the  strtol() function but
47       returns a long long integer value.
48

RETURN VALUE

50       The strtol() function returns the result of the conversion, unless  the
51       value  would  underflow  or overflow.  If an underflow occurs, strtol()
52       returns LONG_MIN.  If an overflow occurs,  strtol()  returns  LONG_MAX.
53       In  both  cases,  errno is set to ERANGE.  Precisely the same holds for
54       strtoll()  (with  LLONG_MIN  and  LLONG_MAX  instead  of  LONG_MIN  and
55       LONG_MAX).
56

ERRORS

58       EINVAL (not in C99) The given base contains an unsupported value.
59
60       ERANGE The resulting value was out of range.
61
62       The  implementation  may also set errno to EINVAL in case no conversion
63       was performed (no digits seen, and 0 returned).
64

CONFORMING TO

66       strtol() conforms to SVr4, 4.3BSD, C89, C99 and POSIX.1-2001, and  str‐
67       toll() to C99 and POSIX.1-2001.
68

NOTES

70       Since  strtol()  can  legitimately  return  0,  LONG_MAX,  or  LONG_MIN
71       (LLONG_MAX or LLONG_MIN for strtoll()) on both success and failure, the
72       calling  program should set errno to 0 before the call, and then deter‐
73       mine if an error occurred by checking whether errno has a nonzero value
74       after the call.
75
76       According  to  POSIX.1-2001, in locales other than the "C" and "POSIX",
77       these  functions  may  accept  other,  implementation-defined   numeric
78       strings.
79
80       BSD also has
81
82           quad_t strtoq(const char *nptr, char **endptr, int base);
83
84       with completely analogous definition.  Depending on the wordsize of the
85       current architecture, this may be equivalent to strtoll()  or  to  str‐
86       tol().
87

EXAMPLE

89       The  program  shown  below demonstrates the use of strtol().  The first
90       command-line argument specifies a string  from  which  strtol()  should
91       parse  a  number.  The second (optional) argument specifies the base to
92       be used for the conversion.  (This argument  is  converted  to  numeric
93       form  using atoi(3), a function that performs no error checking and has
94       a simpler interface than strtol().)  Some examples of the results  pro‐
95       duced by this program are the following:
96
97           $ ./a.out 123
98           strtol() returned 123
99           $ ./a.out '    123'
100           strtol() returned 123
101           $ ./a.out 123abc
102           strtol() returned 123
103           Further characters after number: abc
104           $ ./a.out 123abc 55
105           strtol: Invalid argument
106           $ ./a.out ''
107           No digits were found
108           $ ./a.out 4000000000
109           strtol: Numerical result out of range
110
111   Program source
112
113       #include <stdlib.h>
114       #include <limits.h>
115       #include <stdio.h>
116       #include <errno.h>
117
118       int
119       main(int argc, char *argv[])
120       {
121           int base;
122           char *endptr, *str;
123           long val;
124
125           if (argc < 2) {
126               fprintf(stderr, "Usage: %s str [base]\n", argv[0]);
127               exit(EXIT_FAILURE);
128           }
129
130           str = argv[1];
131           base = (argc > 2) ? atoi(argv[2]) : 10;
132
133           errno = 0;    /* To distinguish success/failure after call */
134           val = strtol(str, &endptr, base);
135
136           /* Check for various possible errors */
137
138           if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
139                   || (errno != 0 && val == 0)) {
140               perror("strtol");
141               exit(EXIT_FAILURE);
142           }
143
144           if (endptr == str) {
145               fprintf(stderr, "No digits were found\n");
146               exit(EXIT_FAILURE);
147           }
148
149           /* If we got here, strtol() successfully parsed a number */
150
151           printf("strtol() returned %ld\n", val);
152
153           if (*endptr != '\0')        /* Not necessarily an error... */
154               printf("Further characters after number: %s\n", endptr);
155
156           exit(EXIT_SUCCESS);
157       }
158

SEE ALSO

160       atof(3), atoi(3), atol(3), strtod(3), strtoul(3)
161

COLOPHON

163       This  page  is  part of release 3.53 of the Linux man-pages project.  A
164       description of the project, and information about reporting  bugs,  can
165       be found at http://www.kernel.org/doc/man-pages/.
166
167
168
169GNU                               2013-02-10                         STRTOL(3)
Impressum