xref: /freebsd/lib/libc/stdlib/strtol.3 (revision 41e0cfabe9b74b0caa676ade60fc177dde080cd5)
158f0484fSRodney W. Grimes.\" Copyright (c) 1990, 1991, 1993
258f0484fSRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
358f0484fSRodney W. Grimes.\"
458f0484fSRodney W. Grimes.\" This code is derived from software contributed to Berkeley by
558f0484fSRodney W. Grimes.\" Chris Torek and the American National Standards Committee X3,
658f0484fSRodney W. Grimes.\" on Information Processing Systems.
758f0484fSRodney W. Grimes.\"
858f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes.\" are met:
1158f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
1658f0484fSRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors
1758f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
1858f0484fSRodney W. Grimes.\"    without specific prior written permission.
1958f0484fSRodney W. Grimes.\"
2058f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2158f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2458f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058f0484fSRodney W. Grimes.\" SUCH DAMAGE.
3158f0484fSRodney W. Grimes.\"
3258f0484fSRodney W. Grimes.\"     @(#)strtol.3	8.1 (Berkeley) 6/4/93
337f3dea24SPeter Wemm.\" $FreeBSD$
3458f0484fSRodney W. Grimes.\"
35f7475de7SRuslan Ermilov.Dd November 28, 2001
3658f0484fSRodney W. Grimes.Dt STRTOL 3
3758f0484fSRodney W. Grimes.Os
3858f0484fSRodney W. Grimes.Sh NAME
399c5cbc30SBill Fenner.Nm strtol , strtoll , strtoimax , strtoq
40f7475de7SRuslan Ermilov.Nd "convert a string value to a"
41f7475de7SRuslan Ermilov.Vt long , "long long" , intmax_t
42f7475de7SRuslan Ermilovor
43f7475de7SRuslan Ermilov.Vt quad_t
44f7475de7SRuslan Ermilovinteger
4525bb73e0SAlexey Zelkin.Sh LIBRARY
4625bb73e0SAlexey Zelkin.Lb libc
4758f0484fSRodney W. Grimes.Sh SYNOPSIS
488aefde06SJeroen Ruigrok van der Werven.In stdlib.h
498aefde06SJeroen Ruigrok van der Werven.In limits.h
5058f0484fSRodney W. Grimes.Ft long
515618f724SRobert Drehmel.Fn strtol "const char * restrict nptr" "char ** restrict endptr" "int base"
524c0440cbSDavid E. O'Brien.Ft long long
535618f724SRobert Drehmel.Fn strtoll "const char * restrict nptr" "char ** restrict endptr" "int base"
549c5cbc30SBill Fenner.In inttypes.h
559c5cbc30SBill Fenner.Ft intmax_t
565618f724SRobert Drehmel.Fn strtoimax "const char * restrict nptr" "char ** restrict endptr" "int base"
578aefde06SJeroen Ruigrok van der Werven.In sys/types.h
588aefde06SJeroen Ruigrok van der Werven.In stdlib.h
598aefde06SJeroen Ruigrok van der Werven.In limits.h
6058f0484fSRodney W. Grimes.Ft quad_t
616333eac6SBruce Evans.Fn strtoq "const char *nptr" "char **endptr" "int base"
6258f0484fSRodney W. Grimes.Sh DESCRIPTION
6358f0484fSRodney W. GrimesThe
6458f0484fSRodney W. Grimes.Fn strtol
6558f0484fSRodney W. Grimesfunction
6658f0484fSRodney W. Grimesconverts the string in
6758f0484fSRodney W. Grimes.Fa nptr
6858f0484fSRodney W. Grimesto a
69f7475de7SRuslan Ermilov.Vt long
7058f0484fSRodney W. Grimesvalue.
7158f0484fSRodney W. GrimesThe
724c0440cbSDavid E. O'Brien.Fn strtoll
734c0440cbSDavid E. O'Brienfunction
744c0440cbSDavid E. O'Brienconverts the string in
754c0440cbSDavid E. O'Brien.Fa nptr
764c0440cbSDavid E. O'Briento a
77f7475de7SRuslan Ermilov.Vt "long long"
784c0440cbSDavid E. O'Brienvalue.
794c0440cbSDavid E. O'BrienThe
809c5cbc30SBill Fenner.Fn strtoimax
819c5cbc30SBill Fennerfunction
829c5cbc30SBill Fennerconverts the string in
839c5cbc30SBill Fenner.Fa nptr
849c5cbc30SBill Fennerto an
85f7475de7SRuslan Ermilov.Vt intmax_t
869c5cbc30SBill Fennervalue.
879c5cbc30SBill FennerThe
8858f0484fSRodney W. Grimes.Fn strtoq
8958f0484fSRodney W. Grimesfunction
9058f0484fSRodney W. Grimesconverts the string in
9158f0484fSRodney W. Grimes.Fa nptr
9258f0484fSRodney W. Grimesto a
93f7475de7SRuslan Ermilov.Vt quad_t
9458f0484fSRodney W. Grimesvalue.
9558f0484fSRodney W. GrimesThe conversion is done according to the given
9658f0484fSRodney W. Grimes.Fa base ,
9758f0484fSRodney W. Grimeswhich must be between 2 and 36 inclusive,
9858f0484fSRodney W. Grimesor be the special value 0.
9958f0484fSRodney W. Grimes.Pp
10058f0484fSRodney W. GrimesThe string may begin with an arbitrary amount of white space
10158f0484fSRodney W. Grimes(as determined by
10258f0484fSRodney W. Grimes.Xr isspace 3 )
10358f0484fSRodney W. Grimesfollowed by a single optional
10458f0484fSRodney W. Grimes.Ql +
10558f0484fSRodney W. Grimesor
10658f0484fSRodney W. Grimes.Ql -
10758f0484fSRodney W. Grimessign.
10858f0484fSRodney W. GrimesIf
10958f0484fSRodney W. Grimes.Fa base
11058f0484fSRodney W. Grimesis zero or 16,
11158f0484fSRodney W. Grimesthe string may then include a
112f7475de7SRuslan Ermilov.Dq Li 0x
11358f0484fSRodney W. Grimesprefix,
11458f0484fSRodney W. Grimesand the number will be read in base 16; otherwise, a zero
11558f0484fSRodney W. Grimes.Fa base
11658f0484fSRodney W. Grimesis taken as 10 (decimal) unless the next character is
11758f0484fSRodney W. Grimes.Ql 0 ,
11858f0484fSRodney W. Grimesin which case it is taken as 8 (octal).
11958f0484fSRodney W. Grimes.Pp
12058f0484fSRodney W. GrimesThe remainder of the string is converted to a
121f7475de7SRuslan Ermilov.Vt long , "long long" , intmax_t
1229c5cbc30SBill Fenneror
123f7475de7SRuslan Ermilov.Vt quad_t
12458f0484fSRodney W. Grimesvalue in the obvious manner,
12558f0484fSRodney W. Grimesstopping at the first character which is not a valid digit
12658f0484fSRodney W. Grimesin the given base.
12758f0484fSRodney W. Grimes(In bases above 10, the letter
12858f0484fSRodney W. Grimes.Ql A
12958f0484fSRodney W. Grimesin either upper or lower case
13058f0484fSRodney W. Grimesrepresents 10,
13158f0484fSRodney W. Grimes.Ql B
13258f0484fSRodney W. Grimesrepresents 11, and so forth, with
13358f0484fSRodney W. Grimes.Ql Z
13458f0484fSRodney W. Grimesrepresenting 35.)
13558f0484fSRodney W. Grimes.Pp
13658f0484fSRodney W. GrimesIf
13758f0484fSRodney W. Grimes.Fa endptr
138f7475de7SRuslan Ermilovis not
139f7475de7SRuslan Ermilov.Dv NULL ,
14058f0484fSRodney W. Grimes.Fn strtol
14158f0484fSRodney W. Grimesstores the address of the first invalid character in
14258f0484fSRodney W. Grimes.Fa *endptr .
14358f0484fSRodney W. GrimesIf there were no digits at all, however,
14458f0484fSRodney W. Grimes.Fn strtol
14558f0484fSRodney W. Grimesstores the original value of
14658f0484fSRodney W. Grimes.Fa nptr
14758f0484fSRodney W. Grimesin
14858f0484fSRodney W. Grimes.Fa *endptr .
14958f0484fSRodney W. Grimes(Thus, if
15058f0484fSRodney W. Grimes.Fa *nptr
15158f0484fSRodney W. Grimesis not
15258f0484fSRodney W. Grimes.Ql \e0
15358f0484fSRodney W. Grimesbut
15458f0484fSRodney W. Grimes.Fa **endptr
15558f0484fSRodney W. Grimesis
15658f0484fSRodney W. Grimes.Ql \e0
15758f0484fSRodney W. Grimeson return, the entire string was valid.)
15858f0484fSRodney W. Grimes.Sh RETURN VALUES
15958f0484fSRodney W. GrimesThe
1609c5cbc30SBill Fenner.Fn strtol ,
1619c5cbc30SBill Fenner.Fn strtoll ,
1629c5cbc30SBill Fenner.Fn strtoimax
163f7475de7SRuslan Ermilovand
1649c5cbc30SBill Fenner.Fn strtoq
165f7475de7SRuslan Ermilovfunctions
166f7475de7SRuslan Ermilovreturn the result of the conversion,
16758f0484fSRodney W. Grimesunless the value would underflow or overflow.
1688fc5ce4dSRuslan ErmilovIf no conversion could be performed, 0 is returned and
1698fc5ce4dSRuslan Ermilovthe global variable
1704e6b1570SAndrey A. Chernov.Va errno
1718fc5ce4dSRuslan Ermilovis set to
172d3083737SAndrey A. Chernov.Er EINVAL
173d3083737SAndrey A. Chernov(the last feature is not portable across all platforms).
1749c5cbc30SBill FennerIf an overflow or underflow occurs,
17558f0484fSRodney W. Grimes.Va errno
17658f0484fSRodney W. Grimesis set to
1779c5cbc30SBill Fenner.Er ERANGE
1789c5cbc30SBill Fennerand the function return value is clamped according
1799c5cbc30SBill Fennerto the following table.
18041e0cfabSRuslan Ermilov.Bl -column -offset indent ".Fn strtoimax" ".Sy underflow" ".Sy overflow"
18141e0cfabSRuslan Ermilov.It Sy Function Ta Sy underflow Ta Sy overflow
1829c5cbc30SBill Fenner.It Fn strtol Ta Dv LONG_MIN Ta Dv LONG_MAX
1839c5cbc30SBill Fenner.It Fn strtoll Ta Dv LLONG_MIN Ta Dv LLONG_MAX
1849c5cbc30SBill Fenner.It Fn strtoimax Ta Dv INTMAX_MIN Ta Dv INTMAX_MAX
1859c5cbc30SBill Fenner.It Fn strtoq Ta Dv LLONG_MIN Ta Dv LLONG_MAX
1869c5cbc30SBill Fenner.El
18758f0484fSRodney W. Grimes.Sh ERRORS
188274b6244SAlexey Zelkin.Bl -tag -width Er
1894e6b1570SAndrey A. Chernov.It Bq Er EINVAL
1908fc5ce4dSRuslan ErmilovThe value of
1918fc5ce4dSRuslan Ermilov.Fa base
1928fc5ce4dSRuslan Ermilovis not supported or
193d3083737SAndrey A. Chernovno conversion could be performed
194d3083737SAndrey A. Chernov(the last feature is not portable across all platforms).
19558f0484fSRodney W. Grimes.It Bq Er ERANGE
19658f0484fSRodney W. GrimesThe given string was out of range; the value converted has been clamped.
19758f0484fSRodney W. Grimes.El
19858f0484fSRodney W. Grimes.Sh SEE ALSO
19958f0484fSRodney W. Grimes.Xr atof 3 ,
20058f0484fSRodney W. Grimes.Xr atoi 3 ,
20158f0484fSRodney W. Grimes.Xr atol 3 ,
20258f0484fSRodney W. Grimes.Xr strtod 3 ,
2033953c117SMaxim Konovalov.Xr strtonum 3 ,
204ecab372bSTim J. Robbins.Xr strtoul 3 ,
205ecab372bSTim J. Robbins.Xr wcstol 3
20658f0484fSRodney W. Grimes.Sh STANDARDS
20758f0484fSRodney W. GrimesThe
20858f0484fSRodney W. Grimes.Fn strtol
20958f0484fSRodney W. Grimesfunction
21058f0484fSRodney W. Grimesconforms to
211588a200cSRuslan Ermilov.St -isoC .
2124c0440cbSDavid E. O'BrienThe
2134c0440cbSDavid E. O'Brien.Fn strtoll
2149c5cbc30SBill Fennerand
2159c5cbc30SBill Fenner.Fn strtoimax
2169c5cbc30SBill Fennerfunctions
2179c5cbc30SBill Fennerconform to
2184c0440cbSDavid E. O'Brien.St -isoC-99 .
219083745b5SDavid E. O'BrienThe
220083745b5SDavid E. O'Brien.Bx
2214c0440cbSDavid E. O'Brien.Fn strtoq
2224c0440cbSDavid E. O'Brienfunction is deprecated.
223