1STRTOL(3P) POSIX Programmer's Manual STRTOL(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 strtol, strtoll — convert a string to a long integer
14
16 #include <stdlib.h>
17
18 long strtol(const char *restrict str, char **restrict endptr, int base);
19 long long strtoll(const char *restrict str, char **restrict endptr,
20 int base)
21
23 The functionality described on this reference page is aligned with the
24 ISO C standard. Any conflict between the requirements described here
25 and the ISO C standard is unintentional. This volume of POSIX.1‐2008
26 defers to the ISO C standard.
27
28 These functions shall convert the initial portion of the string pointed
29 to by str to a type long and long long representation, respectively.
30 First, they decompose the input string into three parts:
31
32 1. An initial, possibly empty, sequence of white-space characters (as
33 specified by isspace())
34
35 2. A subject sequence interpreted as an integer represented in some
36 radix determined by the value of base
37
38 3. A final string of one or more unrecognized characters, including
39 the terminating NUL character of the input string.
40
41 Then they shall attempt to convert the subject sequence to an integer,
42 and return the result.
43
44 If the value of base is 0, the expected form of the subject sequence is
45 that of a decimal constant, octal constant, or hexadecimal constant,
46 any of which may be preceded by a '+' or '−' sign. A decimal constant
47 begins with a non-zero digit, and consists of a sequence of decimal
48 digits. An octal constant consists of the prefix '0' optionally fol‐
49 lowed by a sequence of the digits '0' to '7' only. A hexadecimal con‐
50 stant consists of the prefix 0x or 0X followed by a sequence of the
51 decimal digits and letters 'a' (or 'A') to 'f' (or 'F') with values 10
52 to 15 respectively.
53
54 If the value of base is between 2 and 36, the expected form of the sub‐
55 ject sequence is a sequence of letters and digits representing an inte‐
56 ger with the radix specified by base, optionally preceded by a '+' or
57 '−' sign. The letters from 'a' (or 'A') to 'z' (or 'Z') inclusive are
58 ascribed the values 10 to 35; only letters whose ascribed values are
59 less than that of base are permitted. If the value of base is 16, the
60 characters 0x or 0X may optionally precede the sequence of letters and
61 digits, following the sign if present.
62
63 The subject sequence is defined as the longest initial subsequence of
64 the input string, starting with the first non-white-space character
65 that is of the expected form. The subject sequence shall contain no
66 characters if the input string is empty or consists entirely of white-
67 space characters, or if the first non-white-space character is other
68 than a sign or a permissible letter or digit.
69
70 If the subject sequence has the expected form and the value of base is
71 0, the sequence of characters starting with the first digit shall be
72 interpreted as an integer constant. If the subject sequence has the
73 expected form and the value of base is between 2 and 36, it shall be
74 used as the base for conversion, ascribing to each letter its value as
75 given above. If the subject sequence begins with a minus-sign, the
76 value resulting from the conversion shall be negated. A pointer to the
77 final string shall be stored in the object pointed to by endptr, pro‐
78 vided that endptr is not a null pointer.
79
80 In other than the C or POSIX locales, other implementation-defined sub‐
81 ject sequences may be accepted.
82
83 If the subject sequence is empty or does not have the expected form, no
84 conversion is performed; the value of str shall be stored in the object
85 pointed to by endptr, provided that endptr is not a null pointer.
86
87 These functions shall not change the setting of errno if successful.
88
89 Since 0, {LONG_MIN} or {LLONG_MIN}, and {LONG_MAX} or {LLONG_MAX} are
90 returned on error and are also valid returns on success, an application
91 wishing to check for error situations should set errno to 0, then call
92 strtol() or strtoll(), then check errno.
93
95 Upon successful completion, these functions shall return the converted
96 value, if any. If no conversion could be performed, 0 shall be returned
97 and errno may be set to [EINVAL].
98
99 If the value of base is not supported, 0 shall be returned and errno
100 shall be set to [EINVAL].
101
102 If the correct value is outside the range of representable values,
103 {LONG_MIN}, {LONG_MAX}, {LLONG_MIN}, or {LLONG_MAX} shall be returned
104 (according to the sign of the value), and errno set to [ERANGE].
105
107 These functions shall fail if:
108
109 EINVAL The value of base is not supported.
110
111 ERANGE The value to be returned is not representable.
112
113 These functions may fail if:
114
115 EINVAL No conversion could be performed.
116
117 The following sections are informative.
118
120 None.
121
123 Since the value of *endptr is unspecified if the value of base is not
124 supported, applications should either ensure that base has a supported
125 value (0 or between 2 and 36) before the call, or check for an [EINVAL]
126 error before examining *endptr.
127
129 None.
130
132 None.
133
135 fscanf(), isalpha(), strtod()
136
137 The Base Definitions volume of POSIX.1‐2008, <stdlib.h>
138
140 Portions of this text are reprinted and reproduced in electronic form
141 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
142 -- Portable Operating System Interface (POSIX), The Open Group Base
143 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
144 cal and Electronics Engineers, Inc and The Open Group. (This is
145 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
146 event of any discrepancy between this version and the original IEEE and
147 The Open Group Standard, the original IEEE and The Open Group Standard
148 is the referee document. The original Standard can be obtained online
149 at http://www.unix.org/online.html .
150
151 Any typographical or formatting errors that appear in this page are
152 most likely to have been introduced during the conversion of the source
153 files to man page format. To report such errors, see https://www.ker‐
154 nel.org/doc/man-pages/reporting_bugs.html .
155
156
157
158IEEE/The Open Group 2013 STRTOL(3P)