.TH strtol() "" "" "General Function (libc)"
.PC "Convert string to long integer"
.B "#include <stdlib.h>"
\fBlong strtol(\fIstring, tailptr, base\^\fB)\fR
char *\fIstring\^\fB; char **\fItailptr\^\fB; int \fIbase\^\fB;\fR
.PP
.II "subject sequence"
.II "string, convert to long integer"
.II "long integer, create from string"
.II "convert string to long integer"
.B strtol()
converts the number given in
.I string
to a
.B long
and returns its value;
it is a more general version of the function
.BR atol() .
.B strtol()
also stores a pointer to the first character following the number through
.IR tailptr ,
provided
.I tailptr
does not equal NULL.
.PP
.I base
gives the base of the number being read,
either zero or a value from two to 36.
If the given
.I base
is zero,
.B strtol()
determines an implicit base for the number:
hexadecimal if the number starts with 
.B 0x
or
.BR 0X ,
octal if the number starts with
.BR 0 ,
or decimal otherwise.
Alternatively, you can specify a
.I base
between 2 and 36.
.PP
.B strtol()
parses
.I string
into three portions:
beginning, subject sequence, and tail.
.PP
The
.I beginning
consists of zero or more white-space characters that
begin the string.
.PP
The
.I "subject sequence"
is the portion of the string that
.B strtol()
converts into a
.BR long .
It consists of an optional sign character,
an optional prefix
.B 0x
or
.B 0X
if the
.I base
is 16,
and a nonempty sequence of
.I digits
in the specified base.
For example, if the
.I base
is 16, then
.B strtol()
recognizes numeric characters \*(Ql0\*(Qr to \*(Ql9\*(Qr and
alphabetic characters \*(QlA\*(Qr through \*(QlF\*(Qr
and \*(Qla\*(Qr to \*(Qlf\*(Qr as digits.
It continues to scan until it encounters a nondigit.
.PP
The
.I tail
continues from the end of the subject sequence to the null
character that ends the string.
.PP
.B strtol()
ignores the beginning portion of the string.
It converts the subject sequence to a
.BR long .
Finally,
if
.I tailptr
is not NULL,
it sets the pointer pointed to by
.I tailptr
to the address of the first character of the string's tail.
.PP
.B strtol()
returns a
.B long
representing the value of the subject sequence.
If the input
.I string
does not specify a valid number,
it returns zero
and stores the initial value of
.I string
through
.IR tailptr .
If the number it builds is too large or too small to fit into a
.BR long ,
it sets the global variable
.B errno
to the value of the macro
.B ERANGE
and returns
.B LONG_MAX
or
.BR LONG_MIN ,
respectively.
.SH "See Also"
.Xr "libc" libc
.br
\*(AS, \(sc7.10.1.5
.SH Notes
.B strtol()
ignores initial white space in the input
.IR string .
White space is defined as being all characters so
recognized by the function
.BR isspace() .
