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
12 strtol, strtoll - convert a string to a long integer
13
15 #include <stdlib.h>
16
17 long strtol(const char *restrict str, char **restrict endptr, int
18 base);
19 long long strtoll(const char *restrict str, char **restrict endptr,
20 int base)
21
22
24 These functions shall convert the initial portion of the string pointed
25 to by str to a type long and long long representation, respectively.
26 First, they decompose the input string into three parts:
27
28 1. An initial, possibly empty, sequence of white-space characters (as
29 specified by isspace())
30
31 2. A subject sequence interpreted as an integer represented in some
32 radix determined by the value of base
33
34 3. A final string of one or more unrecognized characters, including
35 the terminating null byte of the input string.
36
37 Then they shall attempt to convert the subject sequence to an integer,
38 and return the result.
39
40 If the value of base is 0, the expected form of the subject sequence is
41 that of a decimal constant, octal constant, or hexadecimal constant,
42 any of which may be preceded by a '+' or '-' sign. A decimal constant
43 begins with a non-zero digit, and consists of a sequence of decimal
44 digits. An octal constant consists of the prefix '0' optionally fol‐
45 lowed by a sequence of the digits '0' to '7' only. A hexadecimal con‐
46 stant consists of the prefix 0x or 0X followed by a sequence of the
47 decimal digits and letters 'a' (or 'A' ) to 'f' (or 'F' ) with values
48 10 to 15 respectively.
49
50 If the value of base is between 2 and 36, the expected form of the sub‐
51 ject sequence is a sequence of letters and digits representing an inte‐
52 ger with the radix specified by base, optionally preceded by a '+' or
53 '-' sign. The letters from 'a' (or 'A' ) to 'z' (or 'Z' ) inclusive are
54 ascribed the values 10 to 35; only letters whose ascribed values are
55 less than that of base are permitted. If the value of base is 16, the
56 characters 0x or 0X may optionally precede the sequence of letters and
57 digits, following the sign if present.
58
59 The subject sequence is defined as the longest initial subsequence of
60 the input string, starting with the first non-white-space character
61 that is of the expected form. The subject sequence shall contain no
62 characters if the input string is empty or consists entirely of white-
63 space characters, or if the first non-white-space character is other
64 than a sign or a permissible letter or digit.
65
66 If the subject sequence has the expected form and the value of base is
67 0, the sequence of characters starting with the first digit shall be
68 interpreted as an integer constant. If the subject sequence has the
69 expected form and the value of base is between 2 and 36, it shall be
70 used as the base for conversion, ascribing to each letter its value as
71 given above. If the subject sequence begins with a minus sign, the
72 value resulting from the conversion shall be negated. A pointer to the
73 final string shall be stored in the object pointed to by endptr, pro‐
74 vided that endptr is not a null pointer.
75
76 In other than the C or POSIX locales, other implementation-defined
77 subject sequences may be accepted.
78
79 If the subject sequence is empty or does not have the expected form, no
80 conversion is performed; the value of str is stored in the object
81 pointed to by endptr, provided that endptr is not a null pointer.
82
83 The strtol() function shall not change the setting of errno if success‐
84 ful.
85
86 Since 0, {LONG_MIN} or {LLONG_MIN}, and {LONG_MAX} or {LLONG_MAX} are
87 returned on error and are also valid returns on success, an application
88 wishing to check for error situations should set errno to 0, then call
89 strtol() or strtoll(), then check errno.
90
92 Upon successful completion, these functions shall return the converted
93 value, if any. If no conversion could be performed, 0 shall be returned
94 and errno may be set to [EINVAL].
95
96 If the correct value is outside the range of representable values,
97 {LONG_MIN}, {LONG_MAX}, {LLONG_MIN}, or {LLONG_MAX} shall be returned
98 (according to the sign of the value), and errno set to [ERANGE].
99
101 These functions shall fail if:
102
103 ERANGE The value to be returned is not representable.
104
105
106 These functions may fail if:
107
108 EINVAL The value of base is not supported.
109
110
111 The following sections are informative.
112
114 None.
115
117 None.
118
120 None.
121
123 None.
124
126 isalpha(), scanf(), strtod(), the Base Definitions volume of
127 IEEE Std 1003.1-2001, <stdlib.h>
128
130 Portions of this text are reprinted and reproduced in electronic form
131 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
132 -- Portable Operating System Interface (POSIX), The Open Group Base
133 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
134 Electrical and Electronics Engineers, Inc and The Open Group. In the
135 event of any discrepancy between this version and the original IEEE and
136 The Open Group Standard, the original IEEE and The Open Group Standard
137 is the referee document. The original Standard can be obtained online
138 at http://www.opengroup.org/unix/online.html .
139
140
141
142IEEE/The Open Group 2003 STRTOL(3P)