xref: /freebsd/lib/libc/stdlib/strtol.3 (revision d30837371060e3faf9ba7dd7db3247fa93a52b1a)
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.\" 3. All advertising materials mentioning features or use of this software
1758f0484fSRodney W. Grimes.\"    must display the following acknowledgement:
1858f0484fSRodney W. Grimes.\"	This product includes software developed by the University of
1958f0484fSRodney W. Grimes.\"	California, Berkeley and its contributors.
2058f0484fSRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors
2158f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
2258f0484fSRodney W. Grimes.\"    without specific prior written permission.
2358f0484fSRodney W. Grimes.\"
2458f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2558f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2658f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2758f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2858f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2958f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3058f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3158f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3258f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3358f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3458f0484fSRodney W. Grimes.\" SUCH DAMAGE.
3558f0484fSRodney W. Grimes.\"
3658f0484fSRodney W. Grimes.\"     @(#)strtol.3	8.1 (Berkeley) 6/4/93
377f3dea24SPeter Wemm.\" $FreeBSD$
3858f0484fSRodney W. Grimes.\"
39f7475de7SRuslan Ermilov.Dd November 28, 2001
4058f0484fSRodney W. Grimes.Dt STRTOL 3
4158f0484fSRodney W. Grimes.Os
4258f0484fSRodney W. Grimes.Sh NAME
439c5cbc30SBill Fenner.Nm strtol , strtoll , strtoimax , strtoq
44f7475de7SRuslan Ermilov.Nd "convert a string value to a"
45f7475de7SRuslan Ermilov.Vt long , "long long" , intmax_t
46f7475de7SRuslan Ermilovor
47f7475de7SRuslan Ermilov.Vt quad_t
48f7475de7SRuslan Ermilovinteger
4925bb73e0SAlexey Zelkin.Sh LIBRARY
5025bb73e0SAlexey Zelkin.Lb libc
5158f0484fSRodney W. Grimes.Sh SYNOPSIS
528aefde06SJeroen Ruigrok van der Werven.In stdlib.h
538aefde06SJeroen Ruigrok van der Werven.In limits.h
5458f0484fSRodney W. Grimes.Ft long
555618f724SRobert Drehmel.Fn strtol "const char * restrict nptr" "char ** restrict endptr" "int base"
564c0440cbSDavid E. O'Brien.Ft long long
575618f724SRobert Drehmel.Fn strtoll "const char * restrict nptr" "char ** restrict endptr" "int base"
589c5cbc30SBill Fenner.In inttypes.h
599c5cbc30SBill Fenner.Ft intmax_t
605618f724SRobert Drehmel.Fn strtoimax "const char * restrict nptr" "char ** restrict endptr" "int base"
618aefde06SJeroen Ruigrok van der Werven.In sys/types.h
628aefde06SJeroen Ruigrok van der Werven.In stdlib.h
638aefde06SJeroen Ruigrok van der Werven.In limits.h
6458f0484fSRodney W. Grimes.Ft quad_t
656333eac6SBruce Evans.Fn strtoq "const char *nptr" "char **endptr" "int base"
6658f0484fSRodney W. Grimes.Sh DESCRIPTION
6758f0484fSRodney W. GrimesThe
6858f0484fSRodney W. Grimes.Fn strtol
6958f0484fSRodney W. Grimesfunction
7058f0484fSRodney W. Grimesconverts the string in
7158f0484fSRodney W. Grimes.Fa nptr
7258f0484fSRodney W. Grimesto a
73f7475de7SRuslan Ermilov.Vt long
7458f0484fSRodney W. Grimesvalue.
7558f0484fSRodney W. GrimesThe
764c0440cbSDavid E. O'Brien.Fn strtoll
774c0440cbSDavid E. O'Brienfunction
784c0440cbSDavid E. O'Brienconverts the string in
794c0440cbSDavid E. O'Brien.Fa nptr
804c0440cbSDavid E. O'Briento a
81f7475de7SRuslan Ermilov.Vt "long long"
824c0440cbSDavid E. O'Brienvalue.
834c0440cbSDavid E. O'BrienThe
849c5cbc30SBill Fenner.Fn strtoimax
859c5cbc30SBill Fennerfunction
869c5cbc30SBill Fennerconverts the string in
879c5cbc30SBill Fenner.Fa nptr
889c5cbc30SBill Fennerto an
89f7475de7SRuslan Ermilov.Vt intmax_t
909c5cbc30SBill Fennervalue.
919c5cbc30SBill FennerThe
9258f0484fSRodney W. Grimes.Fn strtoq
9358f0484fSRodney W. Grimesfunction
9458f0484fSRodney W. Grimesconverts the string in
9558f0484fSRodney W. Grimes.Fa nptr
9658f0484fSRodney W. Grimesto a
97f7475de7SRuslan Ermilov.Vt quad_t
9858f0484fSRodney W. Grimesvalue.
9958f0484fSRodney W. GrimesThe conversion is done according to the given
10058f0484fSRodney W. Grimes.Fa base ,
10158f0484fSRodney W. Grimeswhich must be between 2 and 36 inclusive,
10258f0484fSRodney W. Grimesor be the special value 0.
10358f0484fSRodney W. Grimes.Pp
10458f0484fSRodney W. GrimesThe string may begin with an arbitrary amount of white space
10558f0484fSRodney W. Grimes(as determined by
10658f0484fSRodney W. Grimes.Xr isspace 3 )
10758f0484fSRodney W. Grimesfollowed by a single optional
10858f0484fSRodney W. Grimes.Ql +
10958f0484fSRodney W. Grimesor
11058f0484fSRodney W. Grimes.Ql -
11158f0484fSRodney W. Grimessign.
11258f0484fSRodney W. GrimesIf
11358f0484fSRodney W. Grimes.Fa base
11458f0484fSRodney W. Grimesis zero or 16,
11558f0484fSRodney W. Grimesthe string may then include a
116f7475de7SRuslan Ermilov.Dq Li 0x
11758f0484fSRodney W. Grimesprefix,
11858f0484fSRodney W. Grimesand the number will be read in base 16; otherwise, a zero
11958f0484fSRodney W. Grimes.Fa base
12058f0484fSRodney W. Grimesis taken as 10 (decimal) unless the next character is
12158f0484fSRodney W. Grimes.Ql 0 ,
12258f0484fSRodney W. Grimesin which case it is taken as 8 (octal).
12358f0484fSRodney W. Grimes.Pp
12458f0484fSRodney W. GrimesThe remainder of the string is converted to a
125f7475de7SRuslan Ermilov.Vt long , "long long" , intmax_t
1269c5cbc30SBill Fenneror
127f7475de7SRuslan Ermilov.Vt quad_t
12858f0484fSRodney W. Grimesvalue in the obvious manner,
12958f0484fSRodney W. Grimesstopping at the first character which is not a valid digit
13058f0484fSRodney W. Grimesin the given base.
13158f0484fSRodney W. Grimes(In bases above 10, the letter
13258f0484fSRodney W. Grimes.Ql A
13358f0484fSRodney W. Grimesin either upper or lower case
13458f0484fSRodney W. Grimesrepresents 10,
13558f0484fSRodney W. Grimes.Ql B
13658f0484fSRodney W. Grimesrepresents 11, and so forth, with
13758f0484fSRodney W. Grimes.Ql Z
13858f0484fSRodney W. Grimesrepresenting 35.)
13958f0484fSRodney W. Grimes.Pp
14058f0484fSRodney W. GrimesIf
14158f0484fSRodney W. Grimes.Fa endptr
142f7475de7SRuslan Ermilovis not
143f7475de7SRuslan Ermilov.Dv NULL ,
14458f0484fSRodney W. Grimes.Fn strtol
14558f0484fSRodney W. Grimesstores the address of the first invalid character in
14658f0484fSRodney W. Grimes.Fa *endptr .
14758f0484fSRodney W. GrimesIf there were no digits at all, however,
14858f0484fSRodney W. Grimes.Fn strtol
14958f0484fSRodney W. Grimesstores the original value of
15058f0484fSRodney W. Grimes.Fa nptr
15158f0484fSRodney W. Grimesin
15258f0484fSRodney W. Grimes.Fa *endptr .
15358f0484fSRodney W. Grimes(Thus, if
15458f0484fSRodney W. Grimes.Fa *nptr
15558f0484fSRodney W. Grimesis not
15658f0484fSRodney W. Grimes.Ql \e0
15758f0484fSRodney W. Grimesbut
15858f0484fSRodney W. Grimes.Fa **endptr
15958f0484fSRodney W. Grimesis
16058f0484fSRodney W. Grimes.Ql \e0
16158f0484fSRodney W. Grimeson return, the entire string was valid.)
16258f0484fSRodney W. Grimes.Sh RETURN VALUES
16358f0484fSRodney W. GrimesThe
1649c5cbc30SBill Fenner.Fn strtol ,
1659c5cbc30SBill Fenner.Fn strtoll ,
1669c5cbc30SBill Fenner.Fn strtoimax
167f7475de7SRuslan Ermilovand
1689c5cbc30SBill Fenner.Fn strtoq
169f7475de7SRuslan Ermilovfunctions
170f7475de7SRuslan Ermilovreturn the result of the conversion,
17158f0484fSRodney W. Grimesunless the value would underflow or overflow.
1728fc5ce4dSRuslan ErmilovIf no conversion could be performed, 0 is returned and
1738fc5ce4dSRuslan Ermilovthe global variable
1744e6b1570SAndrey A. Chernov.Va errno
1758fc5ce4dSRuslan Ermilovis set to
176d3083737SAndrey A. Chernov.Er EINVAL
177d3083737SAndrey A. Chernov(the last feature is not portable across all platforms).
1789c5cbc30SBill FennerIf an overflow or underflow occurs,
17958f0484fSRodney W. Grimes.Va errno
18058f0484fSRodney W. Grimesis set to
1819c5cbc30SBill Fenner.Er ERANGE
1829c5cbc30SBill Fennerand the function return value is clamped according
1839c5cbc30SBill Fennerto the following table.
1849c5cbc30SBill Fenner.Bl -column -offset indent ".Fn strtoimax" ".Sy overflow" ".Sy underflow"
1859c5cbc30SBill Fenner.It Sy Function Ta Sy overflow Ta Sy underflow
1869c5cbc30SBill Fenner.It Fn strtol Ta Dv LONG_MIN Ta Dv LONG_MAX
1879c5cbc30SBill Fenner.It Fn strtoll Ta Dv LLONG_MIN Ta Dv LLONG_MAX
1889c5cbc30SBill Fenner.It Fn strtoimax Ta Dv INTMAX_MIN Ta Dv INTMAX_MAX
1899c5cbc30SBill Fenner.It Fn strtoq Ta Dv LLONG_MIN Ta Dv LLONG_MAX
1909c5cbc30SBill Fenner.El
19158f0484fSRodney W. Grimes.Sh ERRORS
192274b6244SAlexey Zelkin.Bl -tag -width Er
1934e6b1570SAndrey A. Chernov.It Bq Er EINVAL
1948fc5ce4dSRuslan ErmilovThe value of
1958fc5ce4dSRuslan Ermilov.Fa base
1968fc5ce4dSRuslan Ermilovis not supported or
197d3083737SAndrey A. Chernovno conversion could be performed
198d3083737SAndrey A. Chernov(the last feature is not portable across all platforms).
19958f0484fSRodney W. Grimes.It Bq Er ERANGE
20058f0484fSRodney W. GrimesThe given string was out of range; the value converted has been clamped.
20158f0484fSRodney W. Grimes.El
20258f0484fSRodney W. Grimes.Sh SEE ALSO
20358f0484fSRodney W. Grimes.Xr atof 3 ,
20458f0484fSRodney W. Grimes.Xr atoi 3 ,
20558f0484fSRodney W. Grimes.Xr atol 3 ,
20658f0484fSRodney W. Grimes.Xr strtod 3 ,
207ecab372bSTim J. Robbins.Xr strtoul 3 ,
208ecab372bSTim J. Robbins.Xr wcstol 3
20958f0484fSRodney W. Grimes.Sh STANDARDS
21058f0484fSRodney W. GrimesThe
21158f0484fSRodney W. Grimes.Fn strtol
21258f0484fSRodney W. Grimesfunction
21358f0484fSRodney W. Grimesconforms to
214588a200cSRuslan Ermilov.St -isoC .
2154c0440cbSDavid E. O'BrienThe
2164c0440cbSDavid E. O'Brien.Fn strtoll
2179c5cbc30SBill Fennerand
2189c5cbc30SBill Fenner.Fn strtoimax
2199c5cbc30SBill Fennerfunctions
2209c5cbc30SBill Fennerconform to
2214c0440cbSDavid E. O'Brien.St -isoC-99 .
222083745b5SDavid E. O'BrienThe
223083745b5SDavid E. O'Brien.Bx
2244c0440cbSDavid E. O'Brien.Fn strtoq
2254c0440cbSDavid E. O'Brienfunction is deprecated.
226