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
12       strtol(const char *nptr, char **endptr, int base);
13
14       long long int
15       strtoll(const char *nptr, char **endptr, int base);
16

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

NOTES

61       Since  strtol()  can  legitimately  return  0,  LONG_MAX,  or  LONG_MIN
62       (LLONG_MAX or LLONG_MIN for strtoll()) on both success and failure, the
63       calling  program should set errno to 0 before the call, and then deter‐
64       mine if an error occurred by checking  whether  errno  has  a  non-zero
65       value after the call.
66
67       In  locales  other  than  the  "C"  locale,  other  strings may also be
68       accepted.  (For example, the thousands separator of the current  locale
69       may be supported.)
70
71       BSD also has
72
73           quad_t
74           strtoq(const char *nptr, char **endptr, int base);
75
76       with completely analogous definition.  Depending on the wordsize of the
77       current architecture, this may be equivalent to strtoll()  or  to  str‐
78       tol().
79

CONFORMING TO

81       strtol()  conforms to SVr4, 4.3BSD, C89, C99 and POSIX.1-2001, and str‐
82       toll() to C99 and POSIX.1-2001.
83

EXAMPLE

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

SEE ALSO

156       atof(3), atoi(3), atol(3), strtod(3), strtoul(3)
157
158
159
160GNU                               2002-05-30                         STRTOL(3)
Impressum