__ goes away
[unix-history] / usr / src / lib / libc / stdlib / strtol.3
CommitLineData
ae59e04c 1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
fa98d382
KB
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
043368e6
KB
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
fa98d382
KB
8.\" %sccs.include.redist.man%
9.\"
1778ab6f 10.\" @(#)strtol.3 5.4 (Berkeley) %G%
fa98d382 11.\"
ae59e04c
CL
12.Dd
13.Dt STRTOL 3
14.Os
15.Sh NAME
1778ab6f
KB
16.Nm strtol, strtoq
17.Nd convert string value to a long or quad_t integer
ae59e04c
CL
18.Sh SYNOPSIS
19.Fd #include <stdlib.h>
20.Fd #include <limits.h>
21.Ft long
22.Fn strtol "char *nptr" "char **endptr" "int base"
1778ab6f
KB
23
24.Fd #include <sys/types.h>
25.Fd #include <stdlib.h>
26.Fd #include <limits.h>
27.Ft quad_t
28.Fn strtoq "char *nptr" "char **endptr" "int base"
ae59e04c
CL
29.Sh DESCRIPTION
30The
31.Fn strtol
32function
fa98d382 33converts the string in
ae59e04c 34.Fa nptr
fa98d382 35to a
ae59e04c 36.Em long
1778ab6f
KB
37value.
38The
39.Fn strtoq
40function
41converts the string in
42.Fa nptr
43to a
44.Em quad_t
45value.
46The conversion is done according to the given
ae59e04c 47.Fa base ,
fa98d382
KB
48which must be between 2 and 36 inclusive,
49or be the special value 0.
ae59e04c 50.Pp
fa98d382
KB
51The string may begin with an arbitrary amount of white space
52(as determined by
ae59e04c
CL
53.Xr isspace 3 )
54followed by a single optional
55.Ql +
56or
57.Ql -
58sign.
fa98d382 59If
ae59e04c 60.Fa base
fa98d382 61is zero or 16,
ae59e04c
CL
62the string may then include a
63.Ql 0x
64prefix,
fa98d382 65and the number will be read in base 16; otherwise, a zero
ae59e04c
CL
66.Fa base
67is taken as 10 (decimal) unless the next character is
68.Ql 0 ,
fa98d382 69in which case it is taken as 8 (octal).
ae59e04c 70.Pp
fa98d382 71The remainder of the string is converted to a
ae59e04c 72.Em long
fa98d382
KB
73value in the obvious manner,
74stopping at the first character which is not a valid digit
75in the given base.
ae59e04c
CL
76(In bases above 10, the letter
77.Ql A
78in either upper or lower case
79represents 10,
80.Ql B
81represents 11, and so forth, with
82.Ql Z
83representing 35.)
84.Pp
fa98d382 85If
ae59e04c 86.Fa endptr
fa98d382 87is non nil,
ae59e04c 88.Fn strtol
fa98d382 89stores the address of the first invalid character in
ae59e04c 90.Fa *endptr .
fa98d382 91If there were no digits at all, however,
ae59e04c 92.Fn strtol
fa98d382 93stores the original value of
ae59e04c 94.Fa nptr
fa98d382 95in
ae59e04c 96.Fa *endptr .
fa98d382 97(Thus, if
ae59e04c
CL
98.Fa *nptr
99is not
100.Ql \e0
101but
102.Fa **endptr
103is
104.Ql \e0
105on return, the entire string was valid.)
106.Sh RETURN VALUES
107The
108.Fn strtol
109function
fa98d382
KB
110returns the result of the conversion,
111unless the value would underflow or overflow.
112If an underflow occurs,
ae59e04c 113.Fn strtol
fa98d382 114returns
ae59e04c 115.Dv LONG_MIN .
fa98d382 116If an overflow occurs,
ae59e04c 117.Fn strtol
fa98d382 118returns
ae59e04c 119.Dv LONG_MAX .
fa98d382 120In both cases,
ae59e04c 121.Va errno
fa98d382 122is set to
ae59e04c
CL
123.Er ERANGE .
124.Sh ERRORS
125.Bl -tag -width [ERANGE]
126.It Bq Er ERANGE
fa98d382 127The given string was out of range; the value converted has been clamped.
ae59e04c
CL
128.El
129.Sh SEE ALSO
130.Xr atof 3 ,
131.Xr atoi 3 ,
132.Xr atol 3 ,
133.Xr strtod 3 ,
134.Xr strtoul 3
135.Sh STANDARDS
136The
137.Fn strtol
138function
139conforms to
140.St -ansiC .
141.Sh BUGS
fa98d382 142Ignores the current locale.