1ATOI(3P) POSIX Programmer's Manual ATOI(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 atoi — convert a string to an integer
14
16 #include <stdlib.h>
17
18 int atoi(const char *str);
19
21 The functionality described on this reference page is aligned with the
22 ISO C standard. Any conflict between the requirements described here
23 and the ISO C standard is unintentional. This volume of POSIX.1‐2008
24 defers to the ISO C standard.
25
26 The call atoi(str) shall be equivalent to:
27
28 (int) strtol(str, (char **)NULL, 10)
29
30 except that the handling of errors may differ. If the value cannot be
31 represented, the behavior is undefined.
32
34 The atoi() function shall return the converted value if the value can
35 be represented.
36
38 No errors are defined.
39
40 The following sections are informative.
41
43 Converting an Argument
44 The following example checks for proper usage of the program. If there
45 is an argument and the decimal conversion of this argument (obtained
46 using atoi()) is greater than 0, then the program has a valid number of
47 minutes to wait for an event.
48
49 #include <stdlib.h>
50 #include <stdio.h>
51 ...
52 int minutes_to_event;
53 ...
54 if (argc < 2 || ((minutes_to_event = atoi (argv[1]))) <= 0) {
55 fprintf(stderr, "Usage: %s minutes\n", argv[0]); exit(1);
56 }
57 ...
58
60 The atoi() function is subsumed by strtol() but is retained because it
61 is used extensively in existing code. If the number is not known to be
62 in range, strtol() should be used because atoi() is not required to
63 perform any error checking.
64
66 None.
67
69 None.
70
72 strtol()
73
74 The Base Definitions volume of POSIX.1‐2008, <stdlib.h>
75
77 Portions of this text are reprinted and reproduced in electronic form
78 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
79 -- Portable Operating System Interface (POSIX), The Open Group Base
80 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
81 cal and Electronics Engineers, Inc and The Open Group. (This is
82 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
83 event of any discrepancy between this version and the original IEEE and
84 The Open Group Standard, the original IEEE and The Open Group Standard
85 is the referee document. The original Standard can be obtained online
86 at http://www.unix.org/online.html .
87
88 Any typographical or formatting errors that appear in this page are
89 most likely to have been introduced during the conversion of the source
90 files to man page format. To report such errors, see https://www.ker‐
91 nel.org/doc/man-pages/reporting_bugs.html .
92
93
94
95IEEE/The Open Group 2013 ATOI(3P)