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           _ISOC99_SOURCE
19               || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
20

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

ATTRIBUTES

65       For   an   explanation   of   the  terms  used  in  this  section,  see
66       attributes(7).
67
68       ┌──────────────────────────────┬───────────────┬────────────────┐
69Interface                     Attribute     Value          
70       ├──────────────────────────────┼───────────────┼────────────────┤
71strtol(), strtoll(), strtoq() │ Thread safety │ MT-Safe locale │
72       └──────────────────────────────┴───────────────┴────────────────┘

CONFORMING TO

74       strtol(): POSIX.1-2001, POSIX.1-2008, C89, C99 SVr4, 4.3BSD.
75
76       strtoll(): POSIX.1-2001, POSIX.1-2008, C99.
77

NOTES

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

EXAMPLE

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

SEE ALSO

168       atof(3), atoi(3), atol(3), strtod(3), strtoimax(3) strtoul(3),
169

COLOPHON

171       This  page  is  part of release 5.04 of the Linux man-pages project.  A
172       description of the project, information about reporting bugs,  and  the
173       latest     version     of     this    page,    can    be    found    at
174       https://www.kernel.org/doc/man-pages/.
175
176
177
178GNU                               2019-10-10                         STRTOL(3)
Impressum