strtoi(3bsd)
π strtoi β bestie, we're converting a string to an intmax_t π
okay so this is a page from the strtoi(3bsd) manual, and it lives in the LOCAL section. it's part of the Utility functions from BSD systems situation, aka libbsd, and you link it with -lbsd. it's a whole vibe. let's go π¦
β¨ NAME β¨
strtoi β convert a string value to an intmax_t integer
π LIBRARY π
Utility functions from BSD systems (libbsd, -lbsd)
πΈ SYNOPSIS πΈ
#include <inttypes.h>
(See libbsd(7) for include usage.)
intmax_t
strtoi(const char * restrict nptr, char ** restrict endptr, int base,
intmax_t lo, intmax_t hi, int *rstatus);
not me telling you to go read libbsd(7) for the include usage tea. it's giving "required reading" but it's like three lines, you got this π«Ά
π DESCRIPTION π
The strtoi() function converts the string in nptr to an intmax_t value. bestie, that's the whole job.
The strtoi() function uses internally strtoimax(3) and ensures that the result is always in the range [ lo .. hi ]. like, always always. it refuses to give you chaos.
and then β this is the iconic part β it always places 0 on success or a conversion status in the rstatus argument, avoiding the errno gymnastics the other functions require. no cap, no errno backflips. the rstatus argument can be NULL if conversion status is to be ignored. your call π
The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional '+' or '-' sign. so like, spaces are fine, one sign is fine, don't get weird with it.
If base is zero or 16, the string may then include a '0x' or '0X' prefix, and the number will be read in base 16. otherwise, a zero base is taken as 10 (decimal) unless the next character is '0', in which case it is taken as 8 (octal). it's giving "autodetect but make it mysterious" β¨
The remainder of the string is converted to an intmax_t value in the obvious manner, stopping at the end of the string or at the first character which is not a valid digit in the given base. (In bases above 10, the letter 'A' in either upper or lower case represents 10, 'B' represents 11, and so forth, with 'Z' representing 35.) so yeah, letters are literally digits sometimes. iykyk π₯
If endptr is non-nil, strtoi() stores the address of the first invalid character in *endptr. If there were no digits at all, however, strtoi() stores the original value of nptr in *endptr. (Thus, if *nptr is not '\0' but **endptr is '\0' on return, the entire string was valid.) π little check, big payoff.
π RETURN VALUES π
The strtoi() function always returns the closest value in the range specified by the lo and hi arguments. always. it will clamp. it will not let you down.
The errno value is guaranteed to be left unchanged. periodt. π
Errors are stored as the conversion status in the rstatus argument.
π EXAMPLES π
The following example will always return a number in [1..99] range no matter what the input is, and warn if the conversion failed. iconic behaviour honestly.
int e;
intmax_t lval = strtoi(buf, NULL, 0, 1, 99, &e);
if (e)
warnc(e, "conversion of `%s' to a number failed, using %jd",
buf, lval);
notice theNULLfor endptr and the&efor rstatus. that's the whole personality of this function in two lines π
π ERRORS π
[ECANCELED]β The string did not contain any characters that were converted. nothing happened. zero. empty. the string showed up and did nothing π[EINVAL]β The base is not between2and36and does not contain the special value0. bestie that base is not real.[ENOTSUP]β The string contained non-numeric characters that did not get converted. In this case, endptr points to the first unconverted character. it got partway and then gave up.[ERANGE]β The given string was out of range; the value converted has been clamped; or the range given was invalid, i.e. lo > hi. clamped. squished. contained.
The range check is more important than the unconverted characters check, and it is performed first. so if a program needs to know if there were unconverted characters when an out of range number has been provided, it needs to supply and test endptr. priorities, bestie π
π¦ SEE ALSO π¦
atof(3), atoi(3), atol(3), atoll(3), strtod(3), strtoimax(3), strtol(3), strtoll(3), strtou(3bsd), strtoul(3), strtoull(3), strtoumax(3)
like, the whole friend group. go say hi π«Ά
β STANDARDS β
The strtoi() function is a NetBSD extension. not POSIX, not standard C. it's a NetBSD original β¨
π§ HISTORY π§
The strtoi() function first appeared in NetBSD 7.0.
OpenBSD introduced the strtonum(3bsd) function for the same purpose, but the interface makes it impossible to properly differentiate illegal returns. so like, same goal, worse fit. no shade... okay a tiny bit of shade π
πΈ BUGS πΈ
Ignores the current locale. that's it. that's the bug. it simply does not care about your locale π
π that's the whole page bestie. go forth and clamp your integers responsibly. mwah. π
this page came from Debian, dated January 20, 2024, and it's strtoi(3bsd) β literally the end, ily, bye πβ¨
π¦ where this page comes from
- alpine 3.22: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.23: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.24: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- arch rolling: libbsd 0.12.2-2 Β· custom
- debian trixie: libbsd-dev 0.12.2-2
- epel 10: libbsd-devel 0.12.2-4.el10_0 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- epel 9: libbsd-devel 0.12.2-1.el9 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- nixos 26.05: libbsd 0.12.2
- nixpkgs unstable: libbsd 0.12.2
- ubuntu noble: libbsd-dev 0.12.1-1build1
- ubuntu noble: libbsd-dev 0.12.1-1build1.1
- ubuntu questing: libbsd-dev 0.12.2-2build1
- ubuntu resolute: libbsd-dev 0.12.2-2build2
- ubuntu stonking: libbsd-dev 0.12.2-3
- Copyright (c) 1990, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms