atoi(3)
โจ atoi, atol, atoll โ bestie edition โจ
okay so you want to turn a string into an integer. say less ๐
this is atoi(3), it lives in the Standard C library (libc, link it with -lc), and honestly? she's giving "easy to use, hard to trust." let's get into it ๐ซถ
๐ธ NAME ๐ธ
atoi, atol, atoll โ convert a string to an integer. that's it, that's the whole vibe โจ
๐ LIBRARY ๐
Standard C library (libc, -lc) โ yeah you gotta link her in, no cap.
๐ SYNOPSIS ๐
#include <stdlib.h>
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
๐ฆ Feature Test Macro Requirements for glibc ๐ฆ
(if you're confused what this is, go read feature_test_macros(7), she explains everything ๐)
for atoll():
_ISOC99_SOURCE
|| /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
๐ DESCRIPTION ๐
so atoi() takes the initial portion of the string that nptr points at and converts it to an int. and the behavior is literally identical to:
strtol(nptr, NULL, 10);
except โ and this is the part, this is the part โ atoi() does not detect errors. not even a little. she's not checking anything. she's just vibing ๐ญ
atol() and atoll() do the exact same thing as atoi(), the only difference is they convert that initial portion into their own return type โ long or long long respectively. same energy, bigger container ๐
๐ฅ RETURN VALUE ๐ฅ
the converted value. or 0 on error. (yes, that ambiguity is a whole thing, we'll get to it in BUGS, bestie, don't worry ๐)
โญ ATTRIBUTES โญ
for what all these terms even mean, peep attributes(7) โ she's the source of truth ๐ง
here's the rundown for our girlies:
- Interface:
atoi(),atol(),atoll()- Attribute: Thread safety
- Value:
MT-Safe locale
so yeah, thread-safe, for the locale. slay ๐
๐ VERSIONS ๐
POSIX.1 leaves the return value of atoi() on error unspecified โ like, do whatever, we're not checking ๐คทโโ๏ธ but on glibc, musl libc, and uClibc? 0 is returned on error. consistent queens ๐
๐ STANDARDS ๐
C11, POSIX.1-2008. periodt.
๐ HISTORY ๐
C99, POSIX.1-2001, SVr4, 4.3BSD.
fun little lore drop: C89 and POSIX.1-1996 include the functions atoi() and atol() only. atoll() wasn't even invited yet ๐
๐ BUGS ๐
okay bestie, sit down, this is the part where it gets real ๐
errnois not set on error, so there is literally no way to distinguish between0as an error and0as the converted value. she said "same thing" and meant it ๐ญ- No checks for overflow or underflow are done. none. zero. nada. it'll just do whatever ๐ฅ
- Only base-10 input can be converted. that's the whole menu, babe.
- the recommendation: for new programs, use the
strtol()andstrtoul()family of functions instead. this is them telling you to upgrade, fr fr ๐ซถ
ngl the "recommended to instead usestrtol()andstrtoul()" line is doing a lot of heavy lifting in this section. listen to her. she knows. ๐
๐ฆ SEE ALSO ๐ฆ
and that's the whole page, bestie ๐ this was pulled straight from Linux man-pages 6.20, dated 2026-02-08. go forth, convert some strings, and please please please use strtol() okay? ily, stay safe ๐ซถโจ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms