summaryrefslogtreecommitdiff
path: root/devdocs/c/string%2Fbyte%2Fstrtol.html
blob: 5b16e65b5a59c5e8edc464d46607b6ffd0f234d1 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    <h1 id="firstHeading" class="firstHeading">strtol, strtoll</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;stdlib.h&gt;</code> </th> <th> </th> <th> </th> </tr>   <tr class="t-dcl t-until-c99"> <td><pre data-language="c">long      strtol( const char          *str, char          **str_end, int base );</pre></td> <td class="t-dcl-nopad"> </td> <td><span class="t-mark-rev t-until-c99">(until C99)</span></td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">long      strtol( const char *restrict str, char **restrict str_end, int base );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr>  <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">long long strtoll( const char *restrict str, char **restrict str_end, int base );</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>.</p>
<p>Discards any whitespace characters (as identified by calling <a href="isspace" title="c/string/byte/isspace"><code>isspace</code></a>) until the first non-whitespace character is found, then takes as many characters as possible to form a valid <i>base-n</i> (where n=<code>base</code>) 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> <span class="t-mark">(optional)</span> prefix (<code>0</code>) indicating octal base (applies only when the base is <code>8</code> or <code>​0​</code>) </li>
<li> <span class="t-mark">(optional)</span> prefix (<code>0x</code> or <code>0X</code>) indicating hexadecimal base (applies only when the base is <code>16</code> or <code>​0​</code>) </li>
<li> a sequence of digits </li>
</ul> <p>The set of valid values for base is <code>{0,2,3,...,36}.</code> The set of valid digits for base-<code>2</code> integers is <code>{0,1},</code> for base-<code>3</code> integers is <code>{0,1,2},</code> and so on. For bases larger than <code>10</code>, valid digits include alphabetic characters, starting from <code>Aa</code> for base-<code>11</code> integer, to <code>Zz</code> for base-<code>36</code> integer. The case of the characters is ignored.</p>
<p>Additional numeric formats may be accepted by the currently installed C <a href="../../locale/setlocale" title="c/locale/setlocale">locale</a>.</p>
<p>If the value of <code>base</code> is <code>​0​</code>, the numeric base is auto-detected: if the prefix is <code>0</code>, the base is octal, if the prefix is <code>0x</code> or <code>0X</code>, the base is hexadecimal, otherwise the base is decimal.</p>
<p>If the minus sign was part of the input sequence, the numeric value calculated from the sequence of digits is negated as if by <a href="../../language/operator_arithmetic#Unary_arithmetic" title="c/language/operator arithmetic">unary minus</a> in the result type.</p>
<p>The functions set the pointer pointed to by <code>str_end</code> to point to the character past the last numeric character interpreted. If <code>str_end</code> is a null pointer, it is ignored.</p>
<p>If the <code>str</code> is empty or does not have the expected form, no conversion is performed, and (if <code>str_end</code> is not a null pointer) the value of <code>str</code> is stored in the object pointed to by <code>str_end</code>.</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> <tr class="t-par"> <td> str_end </td> <td> - </td> <td> pointer to a pointer to character. </td>
</tr> <tr class="t-par"> <td> base </td> <td> - </td> <td> <i>base</i> of the interpreted integer value </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <ul>
<li> If successful, an integer value corresponding to the contents of <code>str</code> is returned. </li>
<li> If the converted value falls out of range of corresponding return type, a range error occurs (setting <code><a href="../../error/errno" title="c/error/errno">errno</a></code> to <code><a href="../../error/errno_macros" title="c/error/errno macros">ERANGE</a></code>) and <code><a href="../../types/limits" title="c/types/limits">LONG_MAX</a></code>, <code><a href="../../types/limits" title="c/types/limits">LONG_MIN</a></code>, <code><a href="../../types/limits" title="c/types/limits">LLONG_MAX</a></code> or <code><a href="../../types/limits" title="c/types/limits">LLONG_MIN</a></code> is returned. </li>
<li> If no conversion can be performed, <code>​0​</code> is returned. </li>
</ul> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;errno.h&gt;
#include &lt;limits.h&gt;
#include &lt;stdbool.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
 
int main(void)
{
    // parsing with error handling
    const char *p = "10 200000000000000000000000000000 30 -40 junk";
    printf("Parsing '%s':\n", p);
 
    for (;;)
    {
        // errno can be set to any non-zero value by a library function call
        // regardless of whether there was an error, so it needs to be cleared
        // in order to check the error set by strtol
        errno = 0;
        char *end;
        const long i = strtol(p, &amp;end, 10);
        if (p == end)
            break;
 
        const bool range_error = errno == ERANGE;
        printf("Extracted '%.*s', strtol returned %ld.", (int)(end-p), p, i);
        p = end;
 
        if (range_error)
            printf("\n --&gt; Range error occurred.");
 
        putchar('\n');
    }
 
    printf("Unextracted leftover: '%s'\n\n", p);
 
    // parsing without error handling
    printf("\"1010\" in binary  --&gt; %ld\n", strtol("1010", NULL, 2));
    printf("\"12\"   in octal   --&gt; %ld\n", strtol("12",   NULL, 8));
    printf("\"A\"    in hex     --&gt; %ld\n", strtol("A",    NULL, 16));
    printf("\"junk\" in base-36 --&gt; %ld\n", strtol("junk", NULL, 36));
    printf("\"012\"  in auto-detected base --&gt; %ld\n", strtol("012",  NULL, 0));
    printf("\"0xA\"  in auto-detected base --&gt; %ld\n", strtol("0xA",  NULL, 0));
    printf("\"junk\" in auto-detected base --&gt; %ld\n", strtol("junk", NULL, 0));
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Parsing '10 200000000000000000000000000000 30 -40 junk':
Extracted '10', strtol returned 10.
Extracted ' 200000000000000000000000000000', strtol returned 9223372036854775807.
 --&gt; Range error occurred.
Extracted ' 30', strtol returned 30.
Extracted ' -40', strtol returned -40.
Unextracted leftover: ' junk'
 
"1010" in binary  --&gt; 10
"12"   in octal   --&gt; 10
"A"    in hex     --&gt; 10
"junk" in base-36 --&gt; 926192
"012"  in auto-detected base --&gt; 10
"0xA"  in auto-detected base --&gt; 10
"junk" in auto-detected base --&gt; 0</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.22.1.4 The strtol, strtoll, strtoul, and strtoull functions (p: 251-252) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.22.1.4 The strtol, strtoll, strtoul, and strtoull functions (p: 344-345) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.20.1.4 The strtol, strtoll, strtoul, and strtoull functions (p: 310-311) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.10.1.5 The strtol function </li></ul>
</ul>               <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="atoi" title="c/string/byte/atoi"> <span class="t-lines"><span>atoi</span><span>atol</span><span>atoll</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/strtol" title="cpp/string/byte/strtol">C++ documentation</a></span> for <code>strtol, strtoll</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; 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/strtol" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/strtol</a>
  </p>
</div>