Merge commit 'b31320a79e2054c6739b5229259dbf98f3afc547' into merges
[unleashed.git] / share / man / man3c / strtol.3c
blobea08fe6761c312b3b3811e70a5200765baaa7827
1 '\" te
2 .\" Copyright 1989 AT&T.  Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.  Portions Copyright (c) 2004, Sun Microsystems, Inc.  All Rights Reserved
3 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
4 .\" http://www.opengroup.org/bookstore/.
5 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
6 .\"  This notice shall appear on any product containing this material.
7 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
8 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
9 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
10 .TH STRTOL 3C "May 6, 2003"
11 .SH NAME
12 strtol, strtoll, atol, atoll, atoi, lltostr, ulltostr \- string conversion
13 routines
14 .SH SYNOPSIS
15 .LP
16 .nf
17 #include <stdlib.h>
19 \fBlong\fR \fBstrtol\fR(\fBconst char *restrict\fR \fIstr\fR, \fBchar **restrict\fR \fIendptr\fR, \fBint\fR \fIbase\fR);
20 .fi
22 .LP
23 .nf
24 \fBlong long\fR \fBstrtoll\fR(\fBconst char *restrict\fR \fIstr\fR, \fBchar **restrict\fR \fIendptr\fR,
25      \fBint\fR \fIbase\fR);
26 .fi
28 .LP
29 .nf
30 \fBlong\fR \fBatol\fR(\fBconst char *\fR\fIstr\fR);
31 .fi
33 .LP
34 .nf
35 \fBlong long\fR \fBatoll\fR(\fBconst char *\fR\fIstr\fR);
36 .fi
38 .LP
39 .nf
40 \fBint\fR \fBatoi\fR(\fBconst char *\fR\fIstr\fR);
41 .fi
43 .LP
44 .nf
45 \fBchar *\fR\fBlltostr\fR(\fBlong long\fR \fIvalue\fR, \fBchar *\fR\fIendptr\fR);
46 .fi
48 .LP
49 .nf
50 \fBchar *\fR\fBulltostr\fR(\fBunsigned long long\fR \fIvalue\fR, \fBchar *\fR\fIendptr\fR);
51 .fi
53 .SH DESCRIPTION
54 .SS "\fBstrtol()\fR and \fBstrtoll()\fR"
55 .sp
56 .LP
57 The \fBstrtol()\fR function converts the initial portion of the string pointed
58 to by \fIstr\fR to a type \fBlong int\fR representation.
59 .sp
60 .LP
61 The \fBstrtoll()\fR function converts the initial portion of the string pointed
62 to by \fIstr\fR to a type \fBlong long\fR representation.
63 .sp
64 .LP
65 Both functions first decompose the input string into three parts: an initial,
66 possibly empty, sequence of white-space characters (as specified by
67 \fBisspace\fR(3C)); a subject sequence interpreted as an integer represented in
68 some radix determined by the value of \fIbase\fR; and a final string of one or
69 more unrecognized characters, including the terminating null byte of the input
70 string. They then attempt to convert the subject sequence to an integer and
71 return the result.
72 .sp
73 .LP
74 If the value of \fIbase\fR is 0, the expected form of the subject sequence is
75 that of a decimal constant, octal constant or hexadecimal constant, any of
76 which may be preceded by a + or \(mi sign. A decimal constant begins with a
77 non-zero digit, and consists of a sequence of decimal digits. An octal constant
78 consists of the prefix 0 optionally followed by a sequence of the digits 0 to 7
79 only. A hexadecimal constant consists of the prefix 0x or 0X followed by a
80 sequence of the decimal digits and letters a (or A) to f (or F) with values 10
81 to 15 respectively.
82 .sp
83 .LP
84 If the value of \fIbase\fR is between 2 and 36, the expected form of the
85 subject sequence is a sequence of letters and digits representing an integer
86 with the radix specified by \fIbase\fR, optionally preceded by a + or \(mi
87 sign. The letters from a (or A) to z (or Z) inclusive are ascribed the values
88 10 to 35; only letters whose ascribed values are less than that of \fIbase\fR
89 are permitted. If the value of \fIbase\fR is 16, the characters 0x or 0X may
90 optionally precede the sequence of letters and digits, following the sign if
91 present.
92 .sp
93 .LP
94 The subject sequence is defined as the longest initial subsequence of the input
95 string, starting with the first non-white-space character, that is of the
96 expected form. The subject sequence contains no characters if the input string
97 is empty or consists entirely of white-space characters, or if the first
98 non-white-space character is other than a sign or a permissible letter or
99 digit.
102 If the subject sequence has the expected form and the value of \fIbase\fR is 0,
103 the sequence of characters starting with the first digit is interpreted as an
104 integer constant. If the subject sequence has the expected form and the value
105 of \fIbase\fR is between 2 and 36, it is used as the base for conversion,
106 ascribing to each letter its value as given above. If the subject sequence
107 begins with a minus sign, the value resulting from the conversion is negated. A
108 pointer to the final string is stored in the object pointed to by \fIendptr\fR,
109 provided that \fIendptr\fR is not a null pointer.
112 In other than the POSIX locale, additional implementation-dependent subject
113 sequence forms may be accepted.
116 If the subject sequence is empty or does not have the expected form, no
117 conversion is performed; the value of \fIstr\fR is stored in the object pointed
118 to by \fIendptr\fR, provided that \fIendptr\fR is not a null pointer.
119 .SS "\fBatol()\fR, \fBatoll()\fR and \fBatoi()\fR"
122 Except for behavior on error, \fBatol()\fR is equivalent to: \fBstrtol(str,
123 (char **)NULL, 10)\fR.
126 Except for behavior on error, \fBatoll()\fR is equivalent to: \fBstrtoll(str,
127 (char **)NULL, 10)\fR.
130 Except for behavior on error, \fBatoi()\fR is equivalent to: \fB(int)
131 strtol(str, (char **)NULL, 10)\fR.
134 If the value cannot be represented, the behavior is undefined.
135 .SS "\fBlltostr()\fR and \fBulltostr()\fR"
138 The \fBlltostr()\fR function returns a pointer to the string represented by the
139 \fBlong\fR \fBlong\fR \fIvalue\fR. The \fIendptr\fR argument is assumed to
140 point to the byte following a storage area into which the decimal
141 representation of \fIvalue\fR is to be placed as a string.  The \fBlltostr()\fR
142 function converts \fIvalue\fR to decimal and produces the string,  and returns
143 a pointer to the beginning of the string. No leading zeros are produced, and no
144 terminating null is produced. The low-order digit of the result always occupies
145 memory position  \fIendptr\fR\(mi1. The behavior of \fBlltostr()\fR is
146 undefined if  \fIvalue\fR is negative.  A single zero digit is produced if
147 \fIvalue\fR is 0.
150 The \fBulltostr()\fR function is similar to \fBlltostr()\fR except that
151 \fIvalue\fR is an \fBunsigned long long\fR.
152 .SH RETURN VALUES
155 Upon successful completion, \fBstrtol()\fR, \fBstrtoll()\fR, \fBatol()\fR,
156 \fBatoll()\fR, and \fBatoi()\fR return the converted value, if any. If no
157 conversion could be performed, \fBstrtol()\fR and \fBstrtoll()\fR return
158 \fB0\fR and \fBerrno\fR may be set to \fBEINVAL\fR.
161 If the correct value is outside the range of representable values,
162 \fBstrtol()\fR returns \fBLONG_MAX\fR or \fBLONG_MIN\fR and \fBstrtoll()\fR
163 returns \fBLLONG_MAX\fR or \fBLLONG_MIN\fR (according to the sign of the
164 value), and \fBerrno\fR is set to \fBERANGE\fR.
167 Upon successful completion, \fBlltostr()\fR and \fBulltostr()\fR return a
168 pointer to the converted string.
169 .SH ERRORS
172 The \fBstrtol()\fR and \fBstrtoll()\fR functions will fail if:
174 .ne 2
176 \fB\fBERANGE\fR\fR
178 .RS 10n
179 The value to be returned is not representable. The \fBstrtol()\fR and
180 \fBstrtoll()\fR functions may fail if:
184 .ne 2
186 \fB\fBEINVAL\fR\fR
188 .RS 10n
189 The value of \fIbase\fR is not supported.
192 .SH USAGE
195 Because 0, \fBLONG_MIN\fR, \fBLONG_MAX\fR, \fBLLONG_MIN\fR, and \fBLLONG_MAX\fR
196 are returned on error and are also valid returns on success, an application
197 wishing to check for error situations should set \fBerrno\fR to 0, call the
198 function, then check \fBerrno\fR and if it is non-zero, assume an error has
199 occurred.
202 The \fBstrtol()\fR function no longer accepts values greater than
203 \fBLONG_MAX\fR or \fBLLONG_MAX\fR as valid input. Use \fBstrtoul\fR(3C)
204 instead.
207 Calls to \fBatoi()\fR and \fBatol()\fR might be faster than corresponding calls
208 to \fBstrtol()\fR, and calls to \fBatoll()\fR might be faster than
209 corresponding calls to \fBstrtoll()\fR. However, applications should not use
210 the \fBatoi()\fR, \fBatol()\fR, or \fBatoll()\fR functions unless they know the
211 value represented by the argument will be in range for the corresponding result
212 type.
213 .SH ATTRIBUTES
216 See \fBattributes\fR(5) for descriptions of the following attributes:
221 box;
222 c | c
223 l | l .
224 ATTRIBUTE TYPE  ATTRIBUTE VALUE
226 Interface Stability     See below.
228 MT-Level        MT-Safe
233 The \fBstrtol()\fR, \fBstrtoll()\fR, \fBatol()\fR, \fBatoll()\fR, and
234 \fBatoi()\fR functions are Standard.
235 .SH SEE ALSO
238 \fBisalpha\fR(3C), \fBisspace\fR(3C), \fBscanf\fR(3C), \fBstrtod\fR(3C),
239 \fBstrtoul\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5)