Computer Science: Introduction to C + +: Input/output: Stream and Description

Doorsteptutor material for CBSE is prepared by world's top subject experts: fully solved questions with step-by-step explanation- practice your way to success.

Download PDF of This Page (Size: 164K)

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.

Image of Input/Output (I/O)

Image of Input/Output (I/O)

  • 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.

Image of Input/Output (I/O)-2

Image of Input/Output (I/O)-2

  • 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

Image of Usage of cout and less thanless than

Image of Usage of Cout and Less Thanless Than

Fig. Usage of cout and <<

Image of Input/Output (I/O)-(3)

Image of Input/Output (I/O)-(3)

  • 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

Developed by: