summaryrefslogtreecommitdiff
path: root/devdocs/c/io%2Fstd_streams.html
blob: be45707eec05ba4e0a8d158f5febaf43f13f567a (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
    <h1 id="firstHeading" class="firstHeading">stdin, stdout, stderr</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;stdio.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td> <pre data-language="c">#define stdin  /* implementation-defined */</pre>
</td> <td> (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl"> <td> <pre data-language="c">#define stdout /* implementation-defined */</pre>
</td> <td> (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl"> <td> <pre data-language="c">#define stderr /* implementation-defined */</pre>
</td> <td> (3) </td> <td class="t-dcl-nopad"> </td> </tr>  </table> <p>Three text streams are predefined. These streams are implicitly opened and unoriented at program startup.</p>
<div class="t-li1">
<span class="t-li">1)</span> Associated with the <i>standard input</i> stream, used for reading conventional input. At program startup, the stream is fully buffered if and only if the stream can be determined to not refer to an interactive device.</div> <div class="t-li1">
<span class="t-li">2)</span> Associated with the <i>standard output</i> stream, used for writing conventional output. At program startup, the stream is fully buffered if and only if the stream can be determined to not refer to an interactive device.</div> <div class="t-li1">
<span class="t-li">3)</span> Associated with the <i>standard error</i> stream, used for writing diagnostic output. At program startup, the stream is not fully buffered.</div> <p>What constitutes an interactive device is implementation-defined.</p>
<p>These macros are expanded to expressions of type <code><a href="http://en.cppreference.com/w/c/io/FILE"><span class="kw884">FILE</span></a><span class="sy2">*</span></code>.</p>
<h3 id="Notes"> Notes</h3> <p>Although not mandated by POSIX, the UNIX convention is that <code>stdin</code> and <code>stdout</code> are line-buffered if associated with a terminal and <code>stderr</code> is unbuffered.</p>
<p>These macros may be expanded to modifiable lvalues. If any of these <code><a href="http://en.cppreference.com/w/c/io/FILE"><span class="kw884">FILE</span></a><span class="sy2">*</span></code> lvalue is modified, subsequent operations on the corresponding stream result in unspecified or undefined behavior.</p>
<h3 id="Example"> Example</h3> <div class="t-example">
<p>This example shows a function equivalent to <code><a href="fprintf" title="c/io/fprintf">printf</a></code>.</p>
<div class="c source-c"><pre data-language="c">#include &lt;stdarg.h&gt;
#include &lt;stdio.h&gt;
 
int my_printf(const char * restrict fmt, ...)
{
    va_list vl;
    va_start(vl, fmt);
    int ret = vfprintf(stdout, fmt, vl);
    va_end(vl);
    return ret;
}
 
int main(void)
{
    my_printf("Rounding:\t%f %.0f %.32f\n", 1.5, 1.5, 1.3);
    my_printf("Padding:\t%05.2f %.2f %5.2f\n", 1.5, 1.5, 1.5);
    my_printf("Scientific:\t%E %e\n", 1.5, 1.5);
    my_printf("Hexadecimal:\t%a %A\n", 1.5, 1.5);
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Rounding:       1.500000 2 1.30000000000000004440892098500626
Padding:        01.50 1.50  1.50
Scientific:     1.500000E+00 1.500000e+00
Hexadecimal:    0x1.8p+0 0X1.8P+0</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.21.1 Introduction (p: 217-218) </li>
<li> 7.21.2 Streams (p: 217-219) </li>
<li> 7.21.2 Files (p: 219-221) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.21.1 Introduction (p: 296-298) </li>
<li> 7.21.2 Streams (p: 298-299) </li>
<li> 7.21.2 Files (p: 300-302) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.19.1 Introduction (p: 262-264) </li>
<li> 7.19.2 Streams (p: 264-265) </li>
<li> 7.19.2 Files (p: 266-268) </li>
</ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul>
<li> 7.9.1 Introduction </li>
<li> 7.9.2 Streams </li>
<li> 7.9.3 Files </li>
</ul>
</ul>                       <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="file" title="c/io/FILE"> <span class="t-lines"><span>FILE</span></span></a></div> </td> <td> object type, capable of holding all information needed to control a C I/O stream <br> <span class="t-mark">(typedef)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/io/c/std_streams" title="cpp/io/c/std streams">C++ documentation</a></span> for <code>stdin, stdout, stderr</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/io/std_streams" class="_attribution-link">https://en.cppreference.com/w/c/io/std_streams</a>
  </p>
</div>