Use of I/O Operators

INPUT / OUTPUT statements of C++ can be used for input of data from an input device such as a keyboard or to obtain output on output devices such as VDUs and printers.

In C++, input/output devices are treated as files; files are treated as a sequence or a stream of bytes. In other words, a stream in C++ means a flow of bytes from an input device to the CPU(input stream) and from the CPU to an output device (output stream).

Streams used for input and output are:

Stream Description Meaning

cin console input standard input stream

cout console output standard output stream

cerr standard error standard error stream

With I/O streams following operators are used.

<< The insertion (or put to ) operator is used with the output stream cout.

>> The extraction (or get from) operator is used with the input stream cin.

Note that : cin and cout are also known as predefiened stream objects.

Standard output stream

cout is used for displaying the output on the screen. The put to operator << is required to transfer the contents of the variable to the stream cout. cout recognizes the variable and its data type and displays the output accordingly.

For e.g.

int code = 20; // the variable code is assigned a value 20

cout < code; // Display the value of code i.e 20

Standard input stream

It is used to input the value from the keyboard. the getfrom operator >> is required to get the entered value from cin and store it in a seperate memory location.

For e.g.

float marks; // declaration of the variable

cin >> marks; // read the value from the keyboard

cout << "Marks="

cout << marks; // Display the contents of the variable.

HOME LEARN C++ PREVIOUS NEXT