C++ Output Stream

Default Image

We provide data to the computer for
processing, it called input to the computer. After processing computer
shows us results that called output. Input can be given through
different devices. Keyboard is a standard data input device. In C++ we
can give input to our programme during execution of programme this
process called input to the programme and statements that are used to
take input from user are called input statements. After
processing results are shown and saved on disk. Process of showing
results called output and statements by with the help we get output
called output statements.

               C++ uses a convenient
abstraction called streams to perform input and output operations in
sequential media such as the screen, the keyboard or a file. A stream is
an entity where a program can either insert or extract characters
to/from. There is no need to know details about the media associated to
the stream or any of its internal specifications. All we need to know is
that streams are a source/destination of characters, and that these
characters are provided/accepted sequentially (i.e., one after another).
The standard library defines a handful of stream objects that can be
used to access what are considered the standard sources and destinations
of characters by the environment where the program runs:

























Stream Description
cin Standard input stream
cout Standard output stream
cerr Standard error (output) stream
clog Standard logging (output) stream

Output Stream


            The standard output by
default is the screen, and the C++ stream object defined to contact it
is cout. For organized output operations, cout is used together with the
insertion operator, which is written as << (i.e., two “less than”
signs).


cout << “my college name”;      // prints my college name on screen


cout << 123;                             // prints number 123 on screen


cout << a;                                 // prints the value of a on screen


           The << operator inserts
the data that follows it into the stream that precedes it. In the
examples above, it inserted the precise string my college name, the
number 123, and the value of variable a into the standard output stream cout.
Notice that the sentence in the first statement is enclosed in double
quotes (“) because it is a string, while in the last one, x is not. The
double quoting is what makes the difference; when the text is enclosed
between them, the text is printed same as it is; when they are not, the
text is interpreted as the identifier of a variable, and its value is
printed instead. For example, these two sentences have very different
results:


cout << “area”;             // prints area


cout << area;                // prints value of variable area


cout << “This ” << ” is a ” << “single C++ statement”;


        Multiple insertion operations
(<<) may be chained in a single statement. The last statement
would print the text This is a single C++ statement. Chaining insertions
is especially useful to mix text and variables in a single statement:


            cout << “My age is” << age << ” years and my phone number is” << ph_no;


         Assuming the age variable
contains the value 24 and the ph_no variable contains 12345, the output
of the previous statement would be:


                   My age is 24 years and my phone number is 12345

Read Count: 3 times

Comments

No comments yet.

Leave a Comment