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 ๐Ÿ™ˆ

  • errno is not set on error, so there is literally no way to distinguish between 0 as an error and 0 as 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() and strtoul() family of functions instead. this is them telling you to upgrade, fr fr ๐Ÿซถ
ngl the "recommended to instead use strtol() and strtoul()" 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms