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>

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