xref: /csrg-svn/lib/libc/stdlib/strtol.3 (revision 61180)
1*61180Sbostic.\" Copyright (c) 1990, 1991, 1993
2*61180Sbostic.\"	The Regents of the University of California.  All rights reserved.
342091Sbostic.\"
442091Sbostic.\" This code is derived from software contributed to Berkeley by
550329Sbostic.\" Chris Torek and the American National Standards Committee X3,
650329Sbostic.\" on Information Processing Systems.
750329Sbostic.\"
842091Sbostic.\" %sccs.include.redist.man%
942091Sbostic.\"
10*61180Sbostic.\"     @(#)strtol.3	8.1 (Berkeley) 06/04/93
1142091Sbostic.\"
1248350Scael.Dd
1348350Scael.Dt STRTOL 3
1448350Scael.Os
1548350Scael.Sh NAME
1654428Sbostic.Nm strtol, strtoq
1754428Sbostic.Nd convert string value to a long or quad_t integer
1848350Scael.Sh SYNOPSIS
1948350Scael.Fd #include <stdlib.h>
2048350Scael.Fd #include <limits.h>
2148350Scael.Ft long
2248350Scael.Fn strtol "char *nptr" "char **endptr" "int base"
2354428Sbostic
2454428Sbostic.Fd #include <sys/types.h>
2554428Sbostic.Fd #include <stdlib.h>
2654428Sbostic.Fd #include <limits.h>
2754428Sbostic.Ft quad_t
2854428Sbostic.Fn strtoq "char *nptr" "char **endptr" "int base"
2948350Scael.Sh DESCRIPTION
3048350ScaelThe
3148350Scael.Fn strtol
3248350Scaelfunction
3342091Sbosticconverts the string in
3448350Scael.Fa nptr
3542091Sbosticto a
3648350Scael.Em long
3754428Sbosticvalue.
3854428SbosticThe
3954428Sbostic.Fn strtoq
4054428Sbosticfunction
4154428Sbosticconverts the string in
4254428Sbostic.Fa nptr
4354428Sbosticto a
4454428Sbostic.Em quad_t
4554428Sbosticvalue.
4654428SbosticThe conversion is done according to the given
4748350Scael.Fa base ,
4842091Sbosticwhich must be between 2 and 36 inclusive,
4942091Sbosticor be the special value 0.
5048350Scael.Pp
5142091SbosticThe string may begin with an arbitrary amount of white space
5242091Sbostic(as determined by
5348350Scael.Xr isspace 3 )
5448350Scaelfollowed by a single optional
5548350Scael.Ql +
5648350Scaelor
5748350Scael.Ql -
5848350Scaelsign.
5942091SbosticIf
6048350Scael.Fa base
6142091Sbosticis zero or 16,
6248350Scaelthe string may then include a
6348350Scael.Ql 0x
6448350Scaelprefix,
6542091Sbosticand the number will be read in base 16; otherwise, a zero
6648350Scael.Fa base
6748350Scaelis taken as 10 (decimal) unless the next character is
6848350Scael.Ql 0 ,
6942091Sbosticin which case it is taken as 8 (octal).
7048350Scael.Pp
7142091SbosticThe remainder of the string is converted to a
7248350Scael.Em long
7342091Sbosticvalue in the obvious manner,
7442091Sbosticstopping at the first character which is not a valid digit
7542091Sbosticin the given base.
7648350Scael(In bases above 10, the letter
7748350Scael.Ql A
7848350Scaelin either upper or lower case
7948350Scaelrepresents 10,
8048350Scael.Ql B
8148350Scaelrepresents 11, and so forth, with
8248350Scael.Ql Z
8348350Scaelrepresenting 35.)
8448350Scael.Pp
8542091SbosticIf
8648350Scael.Fa endptr
8742091Sbosticis non nil,
8848350Scael.Fn strtol
8942091Sbosticstores the address of the first invalid character in
9048350Scael.Fa *endptr .
9142091SbosticIf there were no digits at all, however,
9248350Scael.Fn strtol
9342091Sbosticstores the original value of
9448350Scael.Fa nptr
9542091Sbosticin
9648350Scael.Fa *endptr .
9742091Sbostic(Thus, if
9848350Scael.Fa *nptr
9948350Scaelis not
10048350Scael.Ql \e0
10148350Scaelbut
10248350Scael.Fa **endptr
10348350Scaelis
10448350Scael.Ql \e0
10548350Scaelon return, the entire string was valid.)
10648350Scael.Sh RETURN VALUES
10748350ScaelThe
10848350Scael.Fn strtol
10948350Scaelfunction
11042091Sbosticreturns the result of the conversion,
11142091Sbosticunless the value would underflow or overflow.
11242091SbosticIf an underflow occurs,
11348350Scael.Fn strtol
11442091Sbosticreturns
11548350Scael.Dv LONG_MIN .
11642091SbosticIf an overflow occurs,
11748350Scael.Fn strtol
11842091Sbosticreturns
11948350Scael.Dv LONG_MAX .
12042091SbosticIn both cases,
12148350Scael.Va errno
12242091Sbosticis set to
12348350Scael.Er ERANGE .
12448350Scael.Sh ERRORS
12548350Scael.Bl -tag -width [ERANGE]
12648350Scael.It Bq Er ERANGE
12742091SbosticThe given string was out of range; the value converted has been clamped.
12848350Scael.El
12948350Scael.Sh SEE ALSO
13048350Scael.Xr atof 3 ,
13148350Scael.Xr atoi 3 ,
13248350Scael.Xr atol 3 ,
13348350Scael.Xr strtod 3 ,
13448350Scael.Xr strtoul 3
13548350Scael.Sh STANDARDS
13648350ScaelThe
13748350Scael.Fn strtol
13848350Scaelfunction
13948350Scaelconforms to
14048350Scael.St -ansiC .
14148350Scael.Sh BUGS
14242091SbosticIgnores the current locale.
143