[go: up one dir, main page]

0% found this document useful (0 votes)
26 views2 pages

Standard Output (Cout)

cout is the standard output stream in C++ that prints to the screen by default. The insertion operator << is used to output data to cout. Multiple values can be chained together in a single cout statement using << to output text and variables on the same line. cout does not automatically add line breaks, so \n or endl must be used to print on separate lines.

Uploaded by

Ali Ammar Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

Standard Output (Cout)

cout is the standard output stream in C++ that prints to the screen by default. The insertion operator << is used to output data to cout. Multiple values can be chained together in a single cout statement using << to output text and variables on the same line. cout does not automatically add line breaks, so \n or endl must be used to print on separate lines.

Uploaded by

Ali Ammar Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

For formatted output operations, cout is used together with the insertion operator,


which is written as << (i.e., two "less than" signs).

1 cout << "Output sentence"; // prints Output sentence on screen


2 cout << 120; // prints number 120 on screen

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;

Multiple insertion operations (<<) may be chained in a single statement:

  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:

cout << "This is a sentence.";


cout << "This is another sentence.";

The output would be in a single line, without any line breaks in between. Something
like:

This is a sentence.This is another sentence.


To insert a line break, a new-line character shall be inserted at the exact position
the line should be broken. In C++, a new-line character can be specified as \n (i.e.,
a backslash character followed by a lowercase n). For example:

1 cout << "First sentence.\n";


2 cout << "Second sentence.\nThird sentence.";

This produces the following output:

First sentence.
Second sentence.
Third sentence.

Alternatively, the endl manipulator can also be used to break lines. For example:

1 cout << "First sentence." << endl;


2 cout << "Second sentence." << endl;

This would print:

First sentence.
Second sentence.

You might also like