add ANSI contribution notice
[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.\"
043368e6 10.\" @(#)strtol.3 5.3 (Berkeley) %G%
fa98d382 11.\"
ae59e04c
CL
12.Dd
13.Dt STRTOL 3
14.Os
15.Sh NAME
16.Nm strtol
17.Nd convert string value to a long integer
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"
23.Sh DESCRIPTION
24The
25.Fn strtol
26function
fa98d382 27converts the string in
ae59e04c 28.Fa nptr
fa98d382 29to a
ae59e04c 30.Em long
fa98d382 31value according to the given
ae59e04c 32.Fa base ,
fa98d382
KB
33which must be between 2 and 36 inclusive,
34or be the special value 0.
ae59e04c 35.Pp
fa98d382
KB
36The string may begin with an arbitrary amount of white space
37(as determined by
ae59e04c
CL
38.Xr isspace 3 )
39followed by a single optional
40.Ql +
41or
42.Ql -
43sign.
fa98d382 44If
ae59e04c 45.Fa base
fa98d382 46is zero or 16,
ae59e04c
CL
47the string may then include a
48.Ql 0x
49prefix,
fa98d382 50and the number will be read in base 16; otherwise, a zero
ae59e04c
CL
51.Fa base
52is taken as 10 (decimal) unless the next character is
53.Ql 0 ,
fa98d382 54in which case it is taken as 8 (octal).
ae59e04c 55.Pp
fa98d382 56The remainder of the string is converted to a
ae59e04c 57.Em long
fa98d382
KB
58value in the obvious manner,
59stopping at the first character which is not a valid digit
60in the given base.
ae59e04c
CL
61(In bases above 10, the letter
62.Ql A
63in either upper or lower case
64represents 10,
65.Ql B
66represents 11, and so forth, with
67.Ql Z
68representing 35.)
69.Pp
fa98d382 70If
ae59e04c 71.Fa endptr
fa98d382 72is non nil,
ae59e04c 73.Fn strtol
fa98d382 74stores the address of the first invalid character in
ae59e04c 75.Fa *endptr .
fa98d382 76If there were no digits at all, however,
ae59e04c 77.Fn strtol
fa98d382 78stores the original value of
ae59e04c 79.Fa nptr
fa98d382 80in
ae59e04c 81.Fa *endptr .
fa98d382 82(Thus, if
ae59e04c
CL
83.Fa *nptr
84is not
85.Ql \e0
86but
87.Fa **endptr
88is
89.Ql \e0
90on return, the entire string was valid.)
91.Sh RETURN VALUES
92The
93.Fn strtol
94function
fa98d382
KB
95returns the result of the conversion,
96unless the value would underflow or overflow.
97If an underflow occurs,
ae59e04c 98.Fn strtol
fa98d382 99returns
ae59e04c 100.Dv LONG_MIN .
fa98d382 101If an overflow occurs,
ae59e04c 102.Fn strtol
fa98d382 103returns
ae59e04c 104.Dv LONG_MAX .
fa98d382 105In both cases,
ae59e04c 106.Va errno
fa98d382 107is set to
ae59e04c
CL
108.Er ERANGE .
109.Sh ERRORS
110.Bl -tag -width [ERANGE]
111.It Bq Er ERANGE
fa98d382 112The given string was out of range; the value converted has been clamped.
ae59e04c
CL
113.El
114.Sh SEE ALSO
115.Xr atof 3 ,
116.Xr atoi 3 ,
117.Xr atol 3 ,
118.Xr strtod 3 ,
119.Xr strtoul 3
120.Sh STANDARDS
121The
122.Fn strtol
123function
124conforms to
125.St -ansiC .
126.Sh BUGS
fa98d382 127Ignores the current locale.