[iostream.objects] # 31 Input/output library [[input.output]](./#input.output) ## 31.4 Standard iostream objects [iostream.objects] ### [31.4.1](#iostream.syn) Header synopsis [[iostream.syn]](iostream.syn) [🔗](#header:%3ciostream%3e) #include // see [[ios.syn]](ios.syn "31.5.1 Header synopsis")#include // see [[streambuf.syn]](streambuf.syn "31.6.1 Header synopsis")#include // see [[istream.syn]](istream.syn "31.7.1 Header synopsis")#include // see [[ostream.syn]](ostream.syn "31.7.2 Header synopsis")namespace std {extern istream cin; extern ostream cout; extern ostream cerr; extern ostream clog; extern wistream wcin; extern wostream wcout; extern wostream wcerr; extern wostream wclog;} ### [31.4.2](#overview) Overview [[iostream.objects.overview]](iostream.objects.overview) [1](#overview-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L468) In this Clause, the type name FILE refers to the type FILE declared in [](cstdio.syn#header:%3ccstdio%3e "31.13.1 Header synopsis [cstdio.syn]")[.](#overview-1.sentence-1) [2](#overview-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L472) The header [](#header:%3ciostream%3e "31.4.1 Header synopsis [iostream.syn]") declares objects that associate objects with the standard C streams provided for by the functions declared in[](cstdio.syn#header:%3ccstdio%3e "31.13.1 Header synopsis [cstdio.syn]"), and includes all the headers necessary to use these objects[.](#overview-2.sentence-1) The dynamic types of the stream buffers initially associated with these objects are unspecified, but they have the behavior specified forstd​::​basic_filebuf orstd​::​basic_filebuf[.](#overview-2.sentence-2) [3](#overview-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L484) The objects are constructed and the associations are established at some time prior to or during the first time an object of classios_base​::​Init is constructed, and in any case before the body of main ([[basic.start.main]](basic.start.main "6.10.3.1 main function")) begins execution[.](#overview-3.sentence-1) The objects are not destroyed during program execution[.](#overview-3.sentence-2)[258](#footnote-258 "Constructors and destructors for objects with static storage duration can access these objects to read input from stdin or write output to stdout or stderr.") [4](#overview-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L501) *Recommended practice*: If it is possible for them to do so, implementations should initialize the objects earlier than required[.](#overview-4.sentence-1) [5](#overview-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L506) The results of including [](#header:%3ciostream%3e "31.4.1 Header synopsis [iostream.syn]") in a translation unit shall be as if[](#header:%3ciostream%3e "31.4.1 Header synopsis [iostream.syn]") defined an instance of ios_base​::​Init with static storage duration[.](#overview-5.sentence-1) Each C++ library module ([[std.modules]](std.modules "16.4.2.4 Modules")) in a hosted implementation shall behave as if it contains an interface unit that defines an unexported ios_base​::​Init variable with ordered initialization ([[basic.start.dynamic]](basic.start.dynamic "6.10.3.3 Dynamic initialization of non-block variables"))[.](#overview-5.sentence-2) [*Note [1](#overview-note-1)*: As a result, the definition of that variable is appearance-ordered before any declaration following the point of importation of a C++ library module[.](#overview-5.sentence-3) Whether such a definition exists is unobservable by a program that does not reference any of the standard iostream objects[.](#overview-5.sentence-4) — *end note*] [6](#overview-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L522) Mixing operations on corresponding wide- and narrow-character streams follows the same semantics as mixing such operations onFILEs, as specified in the C standard library[.](#overview-6.sentence-1) [7](#overview-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L528) Concurrent access to a synchronized ([[ios.members.static]](ios.members.static "31.5.2.5 Static members")) standard iostream object's formatted and unformatted input ([[istream]](istream "31.7.5.2 Class template basic_­istream")) and output ([[ostream]](ostream "31.7.6.2 Class template basic_­ostream")) functions or a standard C stream by multiple threads does not result in a data race ([[intro.multithread]](intro.multithread "6.10.2 Multi-threaded executions and data races"))[.](#overview-7.sentence-1) [*Note [2](#overview-note-2)*: Unsynchronized concurrent use of these objects and streams by multiple threads can result in interleaved characters[.](#overview-7.sentence-2) — *end note*] See also: ISO/IEC 9899:2024, 7.21.2 [258)](#footnote-258)[258)](#footnoteref-258) Constructors and destructors for objects with static storage duration can access these objects to read input fromstdin or write output tostdout orstderr[.](#footnote-258.sentence-1) ### [31.4.3](#narrow.stream.objects) Narrow stream objects [[narrow.stream.objects]](narrow.stream.objects) [🔗](#lib:cin) `istream cin; ` [1](#narrow.stream.objects-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L545) The object cin controls input from a stream buffer associated with the object stdin, declared in [](cstdio.syn#header:%3ccstdio%3e "31.13.1 Header synopsis [cstdio.syn]")[.](#narrow.stream.objects-1.sentence-1) [2](#narrow.stream.objects-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L550) After the objectcin is initialized,cin.tie() returns&cout[.](#narrow.stream.objects-2.sentence-1) Its state is otherwise the same as required for[basic_ios​::​init](istream.cons#lib:basic_ios,init "31.7.5.2.2 Constructors [istream.cons]")[.](#narrow.stream.objects-2.sentence-2) [🔗](#lib:cout) `ostream cout; ` [3](#narrow.stream.objects-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L567) The object cout controls output to a stream buffer associated with the object stdout, declared in [](cstdio.syn#header:%3ccstdio%3e "31.13.1 Header synopsis [cstdio.syn]")[.](#narrow.stream.objects-3.sentence-1) [🔗](#lib:cerr) `ostream cerr; ` [4](#narrow.stream.objects-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L579) The object cerr controls output to a stream buffer associated with the object stderr, declared in [](cstdio.syn#header:%3ccstdio%3e "31.13.1 Header synopsis [cstdio.syn]")[.](#narrow.stream.objects-4.sentence-1) [5](#narrow.stream.objects-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L584) After the objectcerr is initialized,cerr.flags() & unitbuf is nonzero and cerr.tie() returns &cout[.](#narrow.stream.objects-5.sentence-1) Its state is otherwise the same as required for[basic_ios​::​init](istream.cons#lib:basic_ios,init "31.7.5.2.2 Constructors [istream.cons]")[.](#narrow.stream.objects-5.sentence-2) [🔗](#lib:clog) `ostream clog; ` [6](#narrow.stream.objects-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L600) The object clog controls output to a stream buffer associated with the object stderr, declared in [](cstdio.syn#header:%3ccstdio%3e "31.13.1 Header synopsis [cstdio.syn]")[.](#narrow.stream.objects-6.sentence-1) ### [31.4.4](#wide.stream.objects) Wide stream objects [[wide.stream.objects]](wide.stream.objects) [🔗](#lib:wcin) `wistream wcin; ` [1](#wide.stream.objects-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L615) The object wcin controls input from a stream buffer associated with the object stdin, declared in [](cstdio.syn#header:%3ccstdio%3e "31.13.1 Header synopsis [cstdio.syn]")[.](#wide.stream.objects-1.sentence-1) [2](#wide.stream.objects-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L620) After the objectwcin is initialized,wcin.tie() returns&wcout[.](#wide.stream.objects-2.sentence-1) Its state is otherwise the same as required for[basic_ios​::​init](istream.cons#lib:basic_ios,init "31.7.5.2.2 Constructors [istream.cons]")[.](#wide.stream.objects-2.sentence-2) [🔗](#lib:wcout) `wostream wcout; ` [3](#wide.stream.objects-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L637) The object wcout controls output to a stream buffer associated with the object stdout, declared in [](cstdio.syn#header:%3ccstdio%3e "31.13.1 Header synopsis [cstdio.syn]")[.](#wide.stream.objects-3.sentence-1) [🔗](#lib:wcerr) `wostream wcerr; ` [4](#wide.stream.objects-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L649) The object wcerr controls output to a stream buffer associated with the object stderr, declared in [](cstdio.syn#header:%3ccstdio%3e "31.13.1 Header synopsis [cstdio.syn]")[.](#wide.stream.objects-4.sentence-1) [5](#wide.stream.objects-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L654) After the objectwcerr is initialized,wcerr.flags() & unitbuf is nonzero and wcerr.tie() returns &wcout[.](#wide.stream.objects-5.sentence-1) Its state is otherwise the same as required for[basic_ios​::​init](istream.cons#lib:basic_ios,init "31.7.5.2.2 Constructors [istream.cons]")[.](#wide.stream.objects-5.sentence-2) [🔗](#lib:wclog) `wostream wclog; ` [6](#wide.stream.objects-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L670) The object wclog controls output to a stream buffer associated with the object stderr, declared in [](cstdio.syn#header:%3ccstdio%3e "31.13.1 Header synopsis [cstdio.syn]")[.](#wide.stream.objects-6.sentence-1)