NCERT Computer: Chapter 7 – Introduction to C + + Part 9

Download PDF of This Page (Size: 277K)

11. Input/output (I/O)

C + + supports input/output statements which can be used to feed new data into the computer or obtain output on an output device such as: VDU, printer etc.

It provides both formatted and unformatted stream I/O statements.

The following C + + streams can be used for the input/output purpose.

In addition to the above I/O streams, two operators are also used. The operator << is known as put to or bit wise shift operator. The operator is known as extraction or get from operator.

Let us consider a situation where the user desires to display a message “My first computer” on the screen.

This can be achieved through the following statement:

Once the above statement is carried out by the computer, the message “My first computer” will appear on the screen.

Similarly the following program segment defines a variable sum of integer type, initializes it to value 100 and displays the contents of the variable on the screen.

Once the above program segment is obeyed by the computer, the following output is displayed on the screen:

The value of variable sum = 100

From the above discussion we see that cout is the standard output stream of C++ by virtue of which output can be displayed on the screen.

The user has defined a variable mark of integer type in the first statement and in the second statement he is trying to read a value from the keyboard. Once this set of statements is obeyed

Fig. Usage of cout and <<

By the computer, whatever is typed on the keyboard (say 87) is received by the cin standard input stream. cin then hands over the typed value to get from operator which ultimately stores it in memory location called marks. The contents of this variable can be displayed on the screen by the statements given below:

We can use more than one output or put to operators within the same output statement as shown below. The activity is known as cascading of operators:

The output of this statement will be marks = 87