1*48350Scael.\" Copyright (c) 1990, 1991 The Regents of the University of California. 242091Sbostic.\" All rights reserved. 342091Sbostic.\" 442091Sbostic.\" This code is derived from software contributed to Berkeley by 542091Sbostic.\" Chris Torek. 642091Sbostic.\" %sccs.include.redist.man% 742091Sbostic.\" 8*48350Scael.\" @(#)strtol.3 5.2 (Berkeley) 04/19/91 942091Sbostic.\" 10*48350Scael.Dd 11*48350Scael.Dt STRTOL 3 12*48350Scael.Os 13*48350Scael.Sh NAME 14*48350Scael.Nm strtol 15*48350Scael.Nd convert string value to a long integer 16*48350Scael.Sh SYNOPSIS 17*48350Scael.Fd #include <stdlib.h> 18*48350Scael.Fd #include <limits.h> 19*48350Scael.Ft long 20*48350Scael.Fn strtol "char *nptr" "char **endptr" "int base" 21*48350Scael.Sh DESCRIPTION 22*48350ScaelThe 23*48350Scael.Fn strtol 24*48350Scaelfunction 2542091Sbosticconverts the string in 26*48350Scael.Fa nptr 2742091Sbosticto a 28*48350Scael.Em long 2942091Sbosticvalue according to the given 30*48350Scael.Fa base , 3142091Sbosticwhich must be between 2 and 36 inclusive, 3242091Sbosticor be the special value 0. 33*48350Scael.Pp 3442091SbosticThe string may begin with an arbitrary amount of white space 3542091Sbostic(as determined by 36*48350Scael.Xr isspace 3 ) 37*48350Scaelfollowed by a single optional 38*48350Scael.Ql + 39*48350Scaelor 40*48350Scael.Ql - 41*48350Scaelsign. 4242091SbosticIf 43*48350Scael.Fa base 4442091Sbosticis zero or 16, 45*48350Scaelthe string may then include a 46*48350Scael.Ql 0x 47*48350Scaelprefix, 4842091Sbosticand the number will be read in base 16; otherwise, a zero 49*48350Scael.Fa base 50*48350Scaelis taken as 10 (decimal) unless the next character is 51*48350Scael.Ql 0 , 5242091Sbosticin which case it is taken as 8 (octal). 53*48350Scael.Pp 5442091SbosticThe remainder of the string is converted to a 55*48350Scael.Em long 5642091Sbosticvalue in the obvious manner, 5742091Sbosticstopping at the first character which is not a valid digit 5842091Sbosticin the given base. 59*48350Scael(In bases above 10, the letter 60*48350Scael.Ql A 61*48350Scaelin either upper or lower case 62*48350Scaelrepresents 10, 63*48350Scael.Ql B 64*48350Scaelrepresents 11, and so forth, with 65*48350Scael.Ql Z 66*48350Scaelrepresenting 35.) 67*48350Scael.Pp 6842091SbosticIf 69*48350Scael.Fa endptr 7042091Sbosticis non nil, 71*48350Scael.Fn strtol 7242091Sbosticstores the address of the first invalid character in 73*48350Scael.Fa *endptr . 7442091SbosticIf there were no digits at all, however, 75*48350Scael.Fn strtol 7642091Sbosticstores the original value of 77*48350Scael.Fa nptr 7842091Sbosticin 79*48350Scael.Fa *endptr . 8042091Sbostic(Thus, if 81*48350Scael.Fa *nptr 82*48350Scaelis not 83*48350Scael.Ql \e0 84*48350Scaelbut 85*48350Scael.Fa **endptr 86*48350Scaelis 87*48350Scael.Ql \e0 88*48350Scaelon return, the entire string was valid.) 89*48350Scael.Sh RETURN VALUES 90*48350ScaelThe 91*48350Scael.Fn strtol 92*48350Scaelfunction 9342091Sbosticreturns the result of the conversion, 9442091Sbosticunless the value would underflow or overflow. 9542091SbosticIf an underflow occurs, 96*48350Scael.Fn strtol 9742091Sbosticreturns 98*48350Scael.Dv LONG_MIN . 9942091SbosticIf an overflow occurs, 100*48350Scael.Fn strtol 10142091Sbosticreturns 102*48350Scael.Dv LONG_MAX . 10342091SbosticIn both cases, 104*48350Scael.Va errno 10542091Sbosticis set to 106*48350Scael.Er ERANGE . 107*48350Scael.Sh ERRORS 108*48350Scael.Bl -tag -width [ERANGE] 109*48350Scael.It Bq Er ERANGE 11042091SbosticThe given string was out of range; the value converted has been clamped. 111*48350Scael.El 112*48350Scael.Sh SEE ALSO 113*48350Scael.Xr atof 3 , 114*48350Scael.Xr atoi 3 , 115*48350Scael.Xr atol 3 , 116*48350Scael.Xr strtod 3 , 117*48350Scael.Xr strtoul 3 118*48350Scael.Sh STANDARDS 119*48350ScaelThe 120*48350Scael.Fn strtol 121*48350Scaelfunction 122*48350Scaelconforms to 123*48350Scael.St -ansiC . 124*48350Scael.Sh BUGS 12542091SbosticIgnores the current locale. 126