summaryrefslogtreecommitdiff
path: root/devdocs/c/string%2Fbyte%2Fstrtof.html
blob: b3be49b74630e4d8488dd5631f835e78af83d88f (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
    <h1 id="firstHeading" class="firstHeading">strtof, strtod, strtold</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-since-c99"> <td> <pre data-language="c">float       strtof( const char *restrict str, char **restrict str_end );</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-until-c99"> <td><pre data-language="c">double      strtod( const char          *str, char          **str_end );</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">double      strtod( const char *restrict str, char **restrict str_end );</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 double strtold( const char *restrict str, char **restrict str_end );</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 a floating-point value in a byte string pointed to by <code>str</code>.</p>
<p>Function discards any whitespace characters (as determined by <code><a href="isspace" title="c/string/byte/isspace">isspace</a></code>) until first non-whitespace character is found. Then it takes as many characters as possible to form a valid floating-point representation and converts them to a floating-point value. The valid floating-point value can be one of the following:</p>
<ul>
<li>decimal floating-point expression. It consists of the following parts: </li>
<ul>
<li> <span class="t-mark">(optional)</span> plus or minus sign </li>
<li> nonempty sequence of decimal digits optionally containing decimal-point character (as determined by the current C <a href="../../locale/setlocale" title="c/locale/setlocale">locale</a>) (defines significand) </li>
<li> <span class="t-mark">(optional)</span> <code>e</code> or <code>E</code> followed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent to base 10) </li>
</ul>
</ul>  <table class="t-rev-begin"> <tr class="t-rev t-since-c99">
<td> <ul>
<li>hexadecimal floating-point expression. It consists of the following parts: </li>
<ul>
<li> <span class="t-mark">(optional)</span> plus or minus sign </li>
<li> <code>0x</code> or <code>0X</code> </li>
<li> nonempty sequence of hexadecimal digits optionally containing a decimal-point character (as determined by the current C <a href="../../locale/setlocale" title="c/locale/setlocale">locale</a>) (defines significand) </li>
<li> <span class="t-mark">(optional)</span> <code>p</code> or <code>P</code> followed with optional minus or plus sign and nonempty sequence of decimal digits (defines exponent to base 2) </li>
</ul>
<li> infinity expression. It consists of the following parts: </li>
<ul>
<li> <span class="t-mark">(optional)</span> plus or minus sign </li>
<li> <code>INF</code> or <code>INFINITY</code> ignoring case </li>
</ul>
<li> not-a-number expression. It consists of the following parts: </li>
<ul>
<li> <span class="t-mark">(optional)</span> plus or minus sign </li>
<li> <code>NAN</code> or <code>NAN(</code><i>char_sequence</i><code>)</code> ignoring case of the <code>NAN</code> part. <i>char_sequence</i> can only contain digits, Latin letters, and underscores. The result is a quiet NaN floating-point value. </li>
</ul>
</ul>      </td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
</tr> </table> <ul><li> any other expression that may be accepted by the currently installed C <a href="../../locale/setlocale" title="c/locale/setlocale">locale</a> </li></ul> <p>The functions sets the pointer pointed to by <code>str_end</code> to point to the character past the last character interpreted. If <code>str_end</code> is a null pointer, it is ignored.</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>
</table> <h3 id="Return_value"> Return value</h3> <p>Floating-point value corresponding to the contents of <code>str</code> on success. If the converted value falls out of range of corresponding return type, range error occurs (<code><a href="../../error/errno" title="c/error/errno">errno</a></code> is set to <code><a href="../../error/errno_macros" title="c/error/errno macros">ERANGE</a></code>) and <code><a href="../../numeric/math/huge_val" title="c/numeric/math/HUGE VAL">HUGE_VAL</a></code>, <code><a href="../../numeric/math/huge_val" title="c/numeric/math/HUGE VAL">HUGE_VALF</a></code> or <code><a href="../../numeric/math/huge_val" title="c/numeric/math/HUGE VAL">HUGE_VALL</a></code> is returned. If no conversion can be performed, <code>​0​</code> is returned.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;errno.h&gt;
#include &lt;stdlib.h&gt;
 
int main(void)
{
    // parsing with error handling
    const char *p = "111.11 -2.22 Nan nan(2) inF 0X1.BC70A3D70A3D7P+6  1.18973e+4932zzz";
    printf("Parsing '%s':\n", p);
    char *end;
    for (double f = strtod(p, &amp;end); p != end; f = strtod(p, &amp;end))
    {
        printf("'%.*s' -&gt; ", (int)(end-p), p);
        p = end;
        if (errno == ERANGE){
            printf("range error, got ");
            errno = 0;
        }
        printf("%f\n", f);
    }
 
    // parsing without error handling
    printf("\"  -0.0000000123junk\"  --&gt;  %g\n", strtod("  -0.0000000123junk", NULL));
    printf("\"junk\"                 --&gt;  %g\n", strtod("junk", NULL));
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Parsing '111.11 -2.22 Nan nan(2) inF 0X1.BC70A3D70A3D7P+6  1.18973e+4932zzz':
'111.11' -&gt; 111.110000
' -2.22' -&gt; -2.220000
' Nan' -&gt; nan
' nan(2)' -&gt; nan
' inF' -&gt; inf
' 0X1.BC70A3D70A3D7P+6' -&gt; 111.110000
'  1.18973e+4932' -&gt; range error, got inf
"  -0.0000000123junk"  --&gt;  -1.23e-08
"junk"                 --&gt;  0</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.22.1.3 The strtod, strtof, and strtold functions (p: 249-251) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.22.1.3 The strtod, strtof, and strtold functions (p: 342-344) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.20.1.3 The strtod, strtof, and strtold functions (p: 308-310) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.10.1.4 The strtod function </li></ul>
</ul>               <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="atof" title="c/string/byte/atof"> <span class="t-lines"><span>atof</span></span></a></div> </td> <td> converts a byte string to a floating-point value <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../wide/wcstof" title="c/string/wide/wcstof"> <span class="t-lines"><span>wcstof</span><span>wcstod</span><span>wcstold</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><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 a floating-point 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/strtof" title="cpp/string/byte/strtof">C++ documentation</a></span> for <code>strtof, strtod, strtold</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/strtof" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/strtof</a>
  </p>
</div>