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