ATOI(3P)
β¨ ATOI(3P) β¨
okay bestie, buckle up, we're doing a POSIX Programmer's Manual page today π
this one's from the POSIX Programmer's Manual, and it's about atoi β the little string-to-integer girlie who's been in everyone's codebase since forever. let's get into it π«Ά
πΈ PROLOG πΈ
bestie real talk for a sec, this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ β so if you're on Linux, go peek at the corresponding Linux manual page for the deets on Linux behavior π. or, plot twist, the interface may not be implemented on Linux at all. so likeβ¦ vibe check your platform first, okay? π
π NAME π
atoi β convert a string to an integer π
π SYNOPSIS π
#include <stdlib.h>
int atoi(const char *str);
π¦ DESCRIPTION π¦
so here's the tea β the functionality described on this reference page is aligned with the ISO C standard. any conflict between the requirements described here and the ISO C standard is unintentional β nobody's trying to start drama. this volume of POSIX.1β2017 defers to the ISO C standard, periodt. π
the call atoi(str) shall be equivalent to:
(int) strtol(str, (char **)NULL, 10)
β¦with one lil caveat: the handling of errors may differ. and if the value cannot be represented, the behavior is undefined π β like, genuinely undefined, not in a cute way, in a "the standard literally will not tell you what happens" way. so don't be messy about it.
π RETURN VALUE π
the atoi() function shall return the converted value if the value can be represented. that's it, that's the whole thing. if it can't be representedβ¦ see the undefined behavior note above, bestie, we don't go there π
π ERRORS π
No errors are defined. iconic behavior honestly, zero problems, zero notes.
the following sections are informative β aka they're the extra commentary, not the binding requirements, iykyk π
π EXAMPLES π
Converting an Argument
so this example checks for proper usage of the program. here's the vibe: if there is an argument, and the decimal conversion of this argument (obtained using atoi()) is greater than 0, then the program has a valid number of minutes to wait for an event. slay. β°
#include <stdlib.h>
#include <stdio.h>
...
int minutes_to_event;
...
if (argc < 2 || ((minutes_to_event = atoi (argv[1]))) <= 0) {
fprintf(stderr, "Usage: %s minutes\n", argv[0]); exit(1);
}
...
π APPLICATION USAGE π
okay so the atoi() function is subsumed by strtol() β like, strtol() ate and left no crumbs β but atoi() is retained because it is used extensively in existing code. legacy queen behavior, we respect it π
but fr fr: if the number is not known to be in range, strtol() should be used, because atoi() is not required to perform any error checking. so if you don't already know your input is safe, don't play games with atoi(), use strtol() and check your errors like a responsible main character π«Ά
π§ RATIONALE π§
None. nothing to explain, she simply is. β¨
β FUTURE DIRECTIONS β
None. the future is unknowable and also there are no plans. π
π₯ SEE ALSO π₯
strtol()- The Base Definitions volume of
POSIX.1β2017,<stdlib.h>
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.
in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page bestie!! go forth and convert your strings responsibly, and remember: if it's not known to be in range, strtol() is right there waiting for you π
IEEE/The Open Group β 2017 β ATOI(3P) π that's the source, the version, and the date, mwah, thanks for reading, ily, bye!! πβ¨π¦
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- 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-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms