1strtol(3)                  Library Functions Manual                  strtol(3)
2
3
4

NAME

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

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <stdlib.h>
13
14       long strtol(const char *restrict nptr,
15                   char **restrict endptr, int base);
16       long long strtoll(const char *restrict nptr,
17                   char **restrict endptr, int base);
18
19   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21       strtoll():
22           _ISOC99_SOURCE
23               || /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
24

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

ATTRIBUTES

69       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
70       tributes(7).
71
72       ┌─────────────────────────────────────┬───────────────┬────────────────┐
73Interface                            Attribute     Value          
74       ├─────────────────────────────────────┼───────────────┼────────────────┤
75strtol(), strtoll(), strtoq()        │ Thread safety │ MT-Safe locale │
76       └─────────────────────────────────────┴───────────────┴────────────────┘
77

STANDARDS

79       C11, POSIX.1-2008.
80

HISTORY

82       strtol()
83              POSIX.1-2001, C89, SVr4, 4.3BSD.
84
85       strtoll()
86              POSIX.1-2001, C99.
87

NOTES

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

EXAMPLES

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

SEE ALSO

177       atof(3), atoi(3), atol(3), strtod(3), strtoimax(3), strtoul(3)
178
179
180
181Linux man-pages 6.04              2023-03-30                         strtol(3)
Impressum