ATOI(3POSIX)

πŸ’– PROLOG πŸ’–

okay bestie, real talk first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface may not even be implemented on Linux at all. no cap.


✨ NAME ✨

atoi β€” convert a string to an integer 🫢


🌸 SYNOPSIS 🌸

#include <stdlib.h>

int atoi(const char *str);

πŸ¦‹ DESCRIPTION πŸ¦‹

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. This volume of POSIX.1‐2017 defers to the ISO C standard. (yes, the standard has main character energy and we respect it πŸ’…)

The call atoi(str) shall be equivalent to:

(int) strtol(str, (char **)NULL, 10)

...except that the handling of errors may differ. And if the value cannot be represented, the behavior is undefined. like, undefined undefined. not "maybe kinda off by a bit". undefined. beware. 😭


πŸ‘‘ RETURN VALUE πŸ‘‘

The atoi() function shall return the converted value if the value can be represented. that's it, that's the whole vibe.


πŸ’€ ERRORS πŸ’€

No errors are defined.

The following sections are informative. (translation: the rest is bonus content, not requirements ✨)


πŸŽ€ EXAMPLES πŸŽ€

Converting an Argument

The following example checks for proper usage of the program. 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.

#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);
}
...

periodt. πŸ’…


🌈 APPLICATION USAGE 🌈

The atoi() function is subsumed by strtol() but is retained because it is used extensively in existing code. So like, it's the iconic legacy queen we keep around forever πŸ₯Ή

If the number is not known to be in range, strtol() should be used because atoi() is not required to perform any error checking.

read that again bestie. atoi() does NOT have to check anything for you. it's giving chaos. if you don't know your number is in range, strtol() is your girl fr fr.


🧚 RATIONALE 🧚

None.


⭐ FUTURE DIRECTIONS ⭐

None.


πŸ”₯ 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 .


okay that's the whole page, you ate that πŸ“ this one's brought to you by IEEE/The Open Group, 2017, and yes the page is literally called ATOI(3POSIX) β€” mwah, bye bestie πŸ’–πŸ¦‹βœ¨