1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
<h1 id="firstHeading" class="firstHeading">atoi, atol, atoll</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdlib.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">int atoi( const char *str );</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">long atol( const char *str );</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">long long atoll( const char *str );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> </table> <p>Interprets an integer value in a byte string pointed to by <code>str</code>. The implied radix is always 10.</p>
<p>Discards any whitespace characters until the first non-whitespace character is found, then takes as many characters as possible to form a valid integer number representation and converts them to an integer value. The valid integer value consists of the following parts:</p>
<ul>
<li> <span class="t-mark">(optional)</span> plus or minus sign </li>
<li> numeric digits </li>
</ul> <p>If the value of the result cannot be represented, i.e. the converted value falls out of range of the corresponding return type, the behavior is undefined.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> str </td> <td> - </td> <td> pointer to the null-terminated byte string to be interpreted </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>Integer value corresponding to the contents of <code>str</code> on success.</p>
<p>If no conversion can be performed, <code>0</code> is returned.</p>
<h3 id="Notes"> Notes</h3> <p>The name stands for "ASCII to integer".</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("%i\n", atoi(" -123junk"));
printf("%i\n", atoi(" +321dust"));
printf("%i\n", atoi("0"));
printf("%i\n", atoi("0042")); // treated as a decimal number with leading zeros
printf("%i\n", atoi("0x2A")); // only leading zero is converted discarding "x2A"
printf("%i\n", atoi("junk")); // no conversion can be performed
printf("%i\n", atoi("2147483648")); // UB: out of range of int
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">-123
321
0
42
0
0
-2147483648</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.22.1.2 The atoi, atol, and atoll functions (p: 249) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.22.1.2 The atoi, atol, and atoll functions (p: 341) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.20.1.2 The atoi, atol, and atoll functions (p: 307) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul>
<li> 4.10.1.2 The atoi function </li>
<li> 4.10.1.3 The atol function </li>
</ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="strtol" title="c/string/byte/strtol"> <span class="t-lines"><span>strtol</span><span>strtoll</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> converts a byte string to an integer value <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="strtoul" title="c/string/byte/strtoul"> <span class="t-lines"><span>strtoul</span><span> strtoull</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> converts a byte string to an unsigned integer value <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../wide/wcstol" title="c/string/wide/wcstol"> <span class="t-lines"><span>wcstol</span><span>wcstoll</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> converts a wide string to an integer value <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../wide/wcstoul" title="c/string/wide/wcstoul"> <span class="t-lines"><span>wcstoul</span><span>wcstoull</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> converts a wide string to an unsigned integer value <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/string/byte/atoi" title="cpp/string/byte/atoi">C++ documentation</a></span> for <code>atoi, atol, atoll</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/string/byte/atoi" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/atoi</a>
</p>
</div>
|