Standard Output (Cout)
Standard Output (Cout)
On most program environments, the standard output by default is the screen, and
the C++ stream object defined to access it is cout.
The << operator inserts the data that follows it into the stream that precedes it. In
the examples above, it inserted the literal string Output sentence, the
number 120, and the value of variable x into the standard output stream cout.
Notice that the sentence in the first statement is enclosed in double quotes (")
because it is a string literal, when the text is enclosed between them, the text is
printed literally;
cout << "This " << " is a " << "single C++ statement";
This last statement would print the text This is a single C++ statement.
Chaining insertions is especially useful to mix literals and variables in a single
statement:
What cout does not do automatically is add line breaks at the end, unless instructed
to do so. For example, take the following two statements inserting into cout:
The output would be in a single line, without any line breaks in between. Something
like:
First sentence.
Second sentence.
Third sentence.
First sentence.
Second sentence.