148350Scael.\" 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 5*50329Sbostic.\" Chris Torek and the American National Standards Committee X3, 6*50329Sbostic.\" on Information Processing Systems. 7*50329Sbostic.\" 842091Sbostic.\" %sccs.include.redist.man% 942091Sbostic.\" 10*50329Sbostic.\" @(#)strtol.3 5.3 (Berkeley) 06/29/91 1142091Sbostic.\" 1248350Scael.Dd 1348350Scael.Dt STRTOL 3 1448350Scael.Os 1548350Scael.Sh NAME 1648350Scael.Nm strtol 1748350Scael.Nd convert string value to a long 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" 2348350Scael.Sh DESCRIPTION 2448350ScaelThe 2548350Scael.Fn strtol 2648350Scaelfunction 2742091Sbosticconverts the string in 2848350Scael.Fa nptr 2942091Sbosticto a 3048350Scael.Em long 3142091Sbosticvalue according to the given 3248350Scael.Fa base , 3342091Sbosticwhich must be between 2 and 36 inclusive, 3442091Sbosticor be the special value 0. 3548350Scael.Pp 3642091SbosticThe string may begin with an arbitrary amount of white space 3742091Sbostic(as determined by 3848350Scael.Xr isspace 3 ) 3948350Scaelfollowed by a single optional 4048350Scael.Ql + 4148350Scaelor 4248350Scael.Ql - 4348350Scaelsign. 4442091SbosticIf 4548350Scael.Fa base 4642091Sbosticis zero or 16, 4748350Scaelthe string may then include a 4848350Scael.Ql 0x 4948350Scaelprefix, 5042091Sbosticand the number will be read in base 16; otherwise, a zero 5148350Scael.Fa base 5248350Scaelis taken as 10 (decimal) unless the next character is 5348350Scael.Ql 0 , 5442091Sbosticin which case it is taken as 8 (octal). 5548350Scael.Pp 5642091SbosticThe remainder of the string is converted to a 5748350Scael.Em long 5842091Sbosticvalue in the obvious manner, 5942091Sbosticstopping at the first character which is not a valid digit 6042091Sbosticin the given base. 6148350Scael(In bases above 10, the letter 6248350Scael.Ql A 6348350Scaelin either upper or lower case 6448350Scaelrepresents 10, 6548350Scael.Ql B 6648350Scaelrepresents 11, and so forth, with 6748350Scael.Ql Z 6848350Scaelrepresenting 35.) 6948350Scael.Pp 7042091SbosticIf 7148350Scael.Fa endptr 7242091Sbosticis non nil, 7348350Scael.Fn strtol 7442091Sbosticstores the address of the first invalid character in 7548350Scael.Fa *endptr . 7642091SbosticIf there were no digits at all, however, 7748350Scael.Fn strtol 7842091Sbosticstores the original value of 7948350Scael.Fa nptr 8042091Sbosticin 8148350Scael.Fa *endptr . 8242091Sbostic(Thus, if 8348350Scael.Fa *nptr 8448350Scaelis not 8548350Scael.Ql \e0 8648350Scaelbut 8748350Scael.Fa **endptr 8848350Scaelis 8948350Scael.Ql \e0 9048350Scaelon return, the entire string was valid.) 9148350Scael.Sh RETURN VALUES 9248350ScaelThe 9348350Scael.Fn strtol 9448350Scaelfunction 9542091Sbosticreturns the result of the conversion, 9642091Sbosticunless the value would underflow or overflow. 9742091SbosticIf an underflow occurs, 9848350Scael.Fn strtol 9942091Sbosticreturns 10048350Scael.Dv LONG_MIN . 10142091SbosticIf an overflow occurs, 10248350Scael.Fn strtol 10342091Sbosticreturns 10448350Scael.Dv LONG_MAX . 10542091SbosticIn both cases, 10648350Scael.Va errno 10742091Sbosticis set to 10848350Scael.Er ERANGE . 10948350Scael.Sh ERRORS 11048350Scael.Bl -tag -width [ERANGE] 11148350Scael.It Bq Er ERANGE 11242091SbosticThe given string was out of range; the value converted has been clamped. 11348350Scael.El 11448350Scael.Sh SEE ALSO 11548350Scael.Xr atof 3 , 11648350Scael.Xr atoi 3 , 11748350Scael.Xr atol 3 , 11848350Scael.Xr strtod 3 , 11948350Scael.Xr strtoul 3 12048350Scael.Sh STANDARDS 12148350ScaelThe 12248350Scael.Fn strtol 12348350Scaelfunction 12448350Scaelconforms to 12548350Scael.St -ansiC . 12648350Scael.Sh BUGS 12742091SbosticIgnores the current locale. 128