Input and Output Stream in C++

What is the input and output stream?

C++ provides various libraries using which we can perform input and output operations.
In C++, we can perform these operations on the data sequence known as streams.

  • Input stream: If the direction of flow of the stream is from the input device to the main memory, then it is known as the input stream.
  • Output Stream: On the other hand, if the flow is from main memory to output devices like screens, it is known as an output stream.

Header files for input/output operations provided by C++

  • iostream: iostream stands for standard input-output where there are definitions of objects like cin cout etc.
  • iomanip: It stands for the input-output manipulator. The methods present in the file use this for stream manipulation.
  • fstream: This header file is used for file handling in which it handles the data read and written by the file.

There are two main statements that we use to take input and show output in C++:
cout and cin.

  • Cout: cout is known as the standard output stream. Cout shows output on the expected output device, which is a screen. The cout statement uses the insertion operator (<<) to take the statement.
  • Cin: cin is also known as the standard input stream. Cin is used to take input from the user. It can take any information from the user. Cin uses an extraction operator (>>) to take the input data.

Example cout:

#include <iostream> using namespace std; int main() { char str1[] = ""; cout<<" Welcome to "<< str1 ; return 0; }


string type

Example cin:

#include <iostream> using namespace std; int main() { int age; cout << "Enter your age:\n"; cin >> age; cout << "Your age is: "<< age; return 0; }


string type