xref: /freebsd/lib/libc/stdlib/strtol.3 (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
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.
16580b4d18SEd Maste.\" 3. 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.\"
32*76edfabbSDag-Erling Smørgrav.Dd August 21, 2023
3358f0484fSRodney W. Grimes.Dt STRTOL 3
3458f0484fSRodney W. Grimes.Os
3558f0484fSRodney W. Grimes.Sh NAME
369c5cbc30SBill Fenner.Nm strtol , strtoll , strtoimax , strtoq
37f7475de7SRuslan Ermilov.Nd "convert a string value to a"
38f7475de7SRuslan Ermilov.Vt long , "long long" , intmax_t
39f7475de7SRuslan Ermilovor
40f7475de7SRuslan Ermilov.Vt quad_t
41f7475de7SRuslan Ermilovinteger
4225bb73e0SAlexey Zelkin.Sh LIBRARY
4325bb73e0SAlexey Zelkin.Lb libc
4458f0484fSRodney W. Grimes.Sh SYNOPSIS
458aefde06SJeroen Ruigrok van der Werven.In stdlib.h
468aefde06SJeroen Ruigrok van der Werven.In limits.h
4758f0484fSRodney W. Grimes.Ft long
485618f724SRobert Drehmel.Fn strtol "const char * restrict nptr" "char ** restrict endptr" "int base"
494c0440cbSDavid E. O'Brien.Ft long long
505618f724SRobert Drehmel.Fn strtoll "const char * restrict nptr" "char ** restrict endptr" "int base"
519c5cbc30SBill Fenner.In inttypes.h
529c5cbc30SBill Fenner.Ft intmax_t
535618f724SRobert Drehmel.Fn strtoimax "const char * restrict nptr" "char ** restrict endptr" "int base"
548aefde06SJeroen Ruigrok van der Werven.In sys/types.h
558aefde06SJeroen Ruigrok van der Werven.In stdlib.h
568aefde06SJeroen Ruigrok van der Werven.In limits.h
5758f0484fSRodney W. Grimes.Ft quad_t
586333eac6SBruce Evans.Fn strtoq "const char *nptr" "char **endptr" "int base"
5958f0484fSRodney W. Grimes.Sh DESCRIPTION
6058f0484fSRodney W. GrimesThe
6158f0484fSRodney W. Grimes.Fn strtol
6258f0484fSRodney W. Grimesfunction
6358f0484fSRodney W. Grimesconverts the string in
6458f0484fSRodney W. Grimes.Fa nptr
6558f0484fSRodney W. Grimesto a
66f7475de7SRuslan Ermilov.Vt long
6758f0484fSRodney W. Grimesvalue.
6858f0484fSRodney W. GrimesThe
694c0440cbSDavid E. O'Brien.Fn strtoll
704c0440cbSDavid E. O'Brienfunction
714c0440cbSDavid E. O'Brienconverts the string in
724c0440cbSDavid E. O'Brien.Fa nptr
734c0440cbSDavid E. O'Briento a
74f7475de7SRuslan Ermilov.Vt "long long"
754c0440cbSDavid E. O'Brienvalue.
764c0440cbSDavid E. O'BrienThe
779c5cbc30SBill Fenner.Fn strtoimax
789c5cbc30SBill Fennerfunction
799c5cbc30SBill Fennerconverts the string in
809c5cbc30SBill Fenner.Fa nptr
819c5cbc30SBill Fennerto an
82f7475de7SRuslan Ermilov.Vt intmax_t
839c5cbc30SBill Fennervalue.
849c5cbc30SBill FennerThe
8558f0484fSRodney W. Grimes.Fn strtoq
8658f0484fSRodney W. Grimesfunction
8758f0484fSRodney W. Grimesconverts the string in
8858f0484fSRodney W. Grimes.Fa nptr
8958f0484fSRodney W. Grimesto a
90f7475de7SRuslan Ermilov.Vt quad_t
9158f0484fSRodney W. Grimesvalue.
9258f0484fSRodney W. GrimesThe conversion is done according to the given
9358f0484fSRodney W. Grimes.Fa base ,
9458f0484fSRodney W. Grimeswhich must be between 2 and 36 inclusive,
9558f0484fSRodney W. Grimesor be the special value 0.
9658f0484fSRodney W. Grimes.Pp
9758f0484fSRodney W. GrimesThe string may begin with an arbitrary amount of white space
9858f0484fSRodney W. Grimes(as determined by
9958f0484fSRodney W. Grimes.Xr isspace 3 )
10058f0484fSRodney W. Grimesfollowed by a single optional
10158f0484fSRodney W. Grimes.Ql +
10258f0484fSRodney W. Grimesor
10358f0484fSRodney W. Grimes.Ql -
10458f0484fSRodney W. Grimessign.
10558f0484fSRodney W. GrimesIf
10658f0484fSRodney W. Grimes.Fa base
10758f0484fSRodney W. Grimesis zero or 16,
10858f0484fSRodney W. Grimesthe string may then include a
109*76edfabbSDag-Erling Smørgrav.Dq Li 0b
110*76edfabbSDag-Erling Smørgravprefix, and the number will be read in base 2; or it may include a
111f7475de7SRuslan Ermilov.Dq Li 0x
11258f0484fSRodney W. Grimesprefix,
11358f0484fSRodney W. Grimesand the number will be read in base 16; otherwise, a zero
11458f0484fSRodney W. Grimes.Fa base
11558f0484fSRodney W. Grimesis taken as 10 (decimal) unless the next character is
11658f0484fSRodney W. Grimes.Ql 0 ,
11758f0484fSRodney W. Grimesin which case it is taken as 8 (octal).
11858f0484fSRodney W. Grimes.Pp
11958f0484fSRodney W. GrimesThe remainder of the string is converted to a
120f7475de7SRuslan Ermilov.Vt long , "long long" , intmax_t
1219c5cbc30SBill Fenneror
122f7475de7SRuslan Ermilov.Vt quad_t
12358f0484fSRodney W. Grimesvalue in the obvious manner,
12458f0484fSRodney W. Grimesstopping at the first character which is not a valid digit
12558f0484fSRodney W. Grimesin the given base.
12658f0484fSRodney W. Grimes(In bases above 10, the letter
12758f0484fSRodney W. Grimes.Ql A
12858f0484fSRodney W. Grimesin either upper or lower case
12958f0484fSRodney W. Grimesrepresents 10,
13058f0484fSRodney W. Grimes.Ql B
13158f0484fSRodney W. Grimesrepresents 11, and so forth, with
13258f0484fSRodney W. Grimes.Ql Z
13358f0484fSRodney W. Grimesrepresenting 35.)
13458f0484fSRodney W. Grimes.Pp
13558f0484fSRodney W. GrimesIf
13658f0484fSRodney W. Grimes.Fa endptr
137f7475de7SRuslan Ermilovis not
138f7475de7SRuslan Ermilov.Dv NULL ,
13958f0484fSRodney W. Grimes.Fn strtol
14058f0484fSRodney W. Grimesstores the address of the first invalid character in
14158f0484fSRodney W. Grimes.Fa *endptr .
14258f0484fSRodney W. GrimesIf there were no digits at all, however,
14358f0484fSRodney W. Grimes.Fn strtol
14458f0484fSRodney W. Grimesstores the original value of
14558f0484fSRodney W. Grimes.Fa nptr
14658f0484fSRodney W. Grimesin
14758f0484fSRodney W. Grimes.Fa *endptr .
14858f0484fSRodney W. Grimes(Thus, if
14958f0484fSRodney W. Grimes.Fa *nptr
15058f0484fSRodney W. Grimesis not
15158f0484fSRodney W. Grimes.Ql \e0
15258f0484fSRodney W. Grimesbut
15358f0484fSRodney W. Grimes.Fa **endptr
15458f0484fSRodney W. Grimesis
15558f0484fSRodney W. Grimes.Ql \e0
15658f0484fSRodney W. Grimeson return, the entire string was valid.)
15758f0484fSRodney W. Grimes.Sh RETURN VALUES
15858f0484fSRodney W. GrimesThe
1599c5cbc30SBill Fenner.Fn strtol ,
1609c5cbc30SBill Fenner.Fn strtoll ,
1619c5cbc30SBill Fenner.Fn strtoimax
162f7475de7SRuslan Ermilovand
1639c5cbc30SBill Fenner.Fn strtoq
164f7475de7SRuslan Ermilovfunctions
165f7475de7SRuslan Ermilovreturn the result of the conversion,
16658f0484fSRodney W. Grimesunless the value would underflow or overflow.
1678fc5ce4dSRuslan ErmilovIf no conversion could be performed, 0 is returned and
1688fc5ce4dSRuslan Ermilovthe global variable
1694e6b1570SAndrey A. Chernov.Va errno
1708fc5ce4dSRuslan Ermilovis set to
171d3083737SAndrey A. Chernov.Er EINVAL
172d3083737SAndrey A. Chernov(the last feature is not portable across all platforms).
1739c5cbc30SBill FennerIf an overflow or underflow occurs,
17458f0484fSRodney W. Grimes.Va errno
17558f0484fSRodney W. Grimesis set to
1769c5cbc30SBill Fenner.Er ERANGE
1779c5cbc30SBill Fennerand the function return value is clamped according
1789c5cbc30SBill Fennerto the following table.
17941e0cfabSRuslan Ermilov.Bl -column -offset indent ".Fn strtoimax" ".Sy underflow" ".Sy overflow"
18041e0cfabSRuslan Ermilov.It Sy Function Ta Sy underflow Ta Sy overflow
1819c5cbc30SBill Fenner.It Fn strtol Ta Dv LONG_MIN Ta Dv LONG_MAX
1829c5cbc30SBill Fenner.It Fn strtoll Ta Dv LLONG_MIN Ta Dv LLONG_MAX
1839c5cbc30SBill Fenner.It Fn strtoimax Ta Dv INTMAX_MIN Ta Dv INTMAX_MAX
1849c5cbc30SBill Fenner.It Fn strtoq Ta Dv LLONG_MIN Ta Dv LLONG_MAX
1859c5cbc30SBill Fenner.El
18658f0484fSRodney W. Grimes.Sh ERRORS
187274b6244SAlexey Zelkin.Bl -tag -width Er
1884e6b1570SAndrey A. Chernov.It Bq Er EINVAL
1898fc5ce4dSRuslan ErmilovThe value of
1908fc5ce4dSRuslan Ermilov.Fa base
1918fc5ce4dSRuslan Ermilovis not supported or
192d3083737SAndrey A. Chernovno conversion could be performed
193d3083737SAndrey A. Chernov(the last feature is not portable across all platforms).
19458f0484fSRodney W. Grimes.It Bq Er ERANGE
19558f0484fSRodney W. GrimesThe given string was out of range; the value converted has been clamped.
19658f0484fSRodney W. Grimes.El
19758f0484fSRodney W. Grimes.Sh SEE ALSO
19858f0484fSRodney W. Grimes.Xr atof 3 ,
19958f0484fSRodney W. Grimes.Xr atoi 3 ,
20058f0484fSRodney W. Grimes.Xr atol 3 ,
20158f0484fSRodney W. Grimes.Xr strtod 3 ,
2023953c117SMaxim Konovalov.Xr strtonum 3 ,
203ecab372bSTim J. Robbins.Xr strtoul 3 ,
204ecab372bSTim J. Robbins.Xr wcstol 3
20558f0484fSRodney W. Grimes.Sh STANDARDS
20658f0484fSRodney W. GrimesThe
20758f0484fSRodney W. Grimes.Fn strtol
20858f0484fSRodney W. Grimesfunction
20958f0484fSRodney W. Grimesconforms to
210588a200cSRuslan Ermilov.St -isoC .
2114c0440cbSDavid E. O'BrienThe
2124c0440cbSDavid E. O'Brien.Fn strtoll
2139c5cbc30SBill Fennerand
2149c5cbc30SBill Fenner.Fn strtoimax
2159c5cbc30SBill Fennerfunctions
2169c5cbc30SBill Fennerconform to
2174c0440cbSDavid E. O'Brien.St -isoC-99 .
218083745b5SDavid E. O'BrienThe
219083745b5SDavid E. O'Brien.Bx
2204c0440cbSDavid E. O'Brien.Fn strtoq
2214c0440cbSDavid E. O'Brienfunction is deprecated.
222