Home >>C++ Tutorial >C++ Basic Input/Output

C++ Basic Input/Output

C++ Basic Input/Output

Stream concept is used by the C++ Input/Output operations and sequence of bytes or flow of the data is known as stream that enhances the performance.

Output operations are the operations in the C++ language that basically involves the flow of bytes from the main memory to the devices like display screen, printer, or a network connection.

Input operations are the operations that basically involve the byte flow from device like display screen, printer, or a network connection.

Library Header files in the Input/Output operations

Here are some of the important header files that are used in C++ programming:

Header File Function and Description
<iostream> In order to define the cout, cin and cerr objects that generally correspond to standard output stream, standard error stream and standard input stream respectively, this header file is used.
<fstream> In order to declare services for the user-controlled file processing, this header file is used.
<iomanip> In order to declare the services that are useful for performing the formatted Input/Output, like setw and setprecision, this header file is used.

Standard output stream (cout)

Ostream class has a predefined object that is named as cout. This object is generally connected with the standard output device that is usually being a display screen. To display the output on a console, the cout is used as conjunction with stream insertion operator (<<).

Here is an example of the standard output stream that will explain it better to you:

#include <iostream>  
using namespace std;  
int main( ) 
{  
   char str[] = "Hello world";  
   cout << "Value of str variable  is: " << str << endl;  
}  
Output :Value of str variable is: Hello world

Standard Input Stream (cin)

Istream class has a predefined object that is named as cin. This object is generally connected with the standard input device that is usually being a keyboard. To read the input from a console, the cin is used as conjunction with stream extraction operator (<<).

Here is an example of the standard input stream (cin) that will explain it you in a better way:

#include <iostream>  
using namespace std;  
int main( ) 
{  
  int num;  
   cout << "Enter Your  Roll Number : ";  
   cin >> num;  
   cout << "Your Roll Number  is: " << num << endl;  
}  
Output : Enter Your Roll Number : 101
Your Roll Number is: 101

Standard End Line (endl)

Ostream class has a predefined endl object that is basically used to flush the stream and insert a new line of characters.

Here is an example of the Standard End Line(endl) that will be easy for you to understand:

#include <iostream>  
using namespace std;  
int main( ) 
{  
cout << "Hello <<endl;    
cout << "Users"<<endl;   
cout << "How are you";   
}   
Output :
Hello
Users
How are you