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
|
<h1 id="firstHeading" class="firstHeading">atof</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">double atof( const char* str );</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </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> <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><code>double</code> value corresponding to the contents of <code>str</code> on success. If the converted value falls out of range of the return type, the return value is undefined. If no conversion can be performed, <code>0.0</code> is returned.</p>
<h3 id="Notes"> Notes</h3> <p>The name stands for "ASCII to float".</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdlib.h>
#include <stdio.h>
int main(void)
{
printf("%g\n", atof(" -0.0000000123junk"));
printf("%g\n", atof("0.012"));
printf("%g\n", atof("15e16"));
printf("%g\n", atof("-0x1afp-2"));
printf("%g\n", atof("inF"));
printf("%g\n", atof("Nan"));
printf("%g\n", atof("1.0e+309")); // UB: out of range of double
printf("%g\n", atof("0.0"));
printf("%g\n", atof("junk")); // no conversion can be performed
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">-1.23e-08
0.012
1.5e+17
-107.75
inf
nan
inf
0
0</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.22.1.1 The atof function (p: 341) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.20.1.1 The atof function (p: 307) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.10.1.1 The atof function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="strtof" title="c/string/byte/strtof"> <span class="t-lines"><span>strtof</span><span>strtod</span><span>strtold</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-c99">(C99)</span></span></span></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 colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/string/byte/atof" title="cpp/string/byte/atof">C++ documentation</a></span> for <code>atof</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/atof" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/atof</a>
</p>
</div>
|