Updated June 8, 2023
Introduction to C++ iostream
In C++, we can perform input and output functionality by using Iostream. This stands for input and output, and this uses the stream to perform this functionality. In c++, stream stands or represents a sequence of character or byte which is used to perform io operations. In programming, the language stream contains the address of the destination. This io represents input and output stream.
Syntax of C++ iostream
Below you can see the syntax for input and output stream.
1. Input Stream: In c++, we use ‘cin’ for the input stream, and this is the instance of the istream class in c++. In c++, we use the>> operator with the cin keyword before it. See syntax below;
Example:
cin >> variable_name;
2. Output Stream: In c++, we use ‘cout’ for output stream, and this is the instance of ostream class in c++. In c++, we use << operator with the cout keyword before it. See syntax below;
Example:
cout << variable_name
How iostream works in C++?
As of now, we know iostream is a combination of input and output stream in a programming language. In c++, we use cout and cin to take the user’s input and print the value on the console. In this section, we will discuss more of these two operations. See below;
1. Input Stream: If the sequence of characters or bytes flows from the device to the memory, then this process is called an input stream. for example, Keyboard. This means we are providing any input to the system but not able to see it.
2. Output Stream: If the sequence of characters or the bytes flows or processes into the opposite direction, then the process is known as output stream in a programming language, for example, Screen. Here it is flowing from the main memory to the device, and we are able to see something on the screen. This stream is basically used to show output on the screen.
Operations of C++ iostream
All this io operation is available in the iostream header. cin and cout are also the part of this header only. Let’s discuss the functionality, or we can say operations which are available under this header file iostream see below with one syntax to use them in programming;
1. cin (istream – standard input stream)
In a programming language, we have an istream class, and cin is the instance of this class which is used to process an input stream. By the use of this function, we can take or process the input parameters and assign value to the variable. To use this, we use the ‘>>’ operator with it followed by the variable name. Let’s take one example to understand it better; To use this function, we need to have iostream include as a header of the program; otherwise, we will get an error.
Code:
#include <iostream>
int main()
{
int rollnumber;
cin >> rollnumber;
return 0;
}
In the above example, we are using the cin function from iostream to take the input followed by the function >> operator.
2. cout (ostream – standard output stream)
In the c++ programming language, we have an ostream class, and cout is the instance of this class which is used to process output stream. By the use of this function, we can see the output of the parameters that we have passed. To use this, we need to use the ‘<<‘ operator with it followed by the variable name. Let’s take one example to understand it better. To use this function, we need to have ostream include as a header of the program; otherwise, we will get an error.
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Message to show on screen!!";
return 0;
}
In the above example, we are using a cout function followed by the << operator; this will print this message on the console.
3. clog
This iostream function is used to show the errors that occurred; this is also available inside the iostream header file.
We can see one example to understand its syntax for better understanding see below;
Code:
#include <iostream>
int main()
{
clog << "This is used to show the error in io stream.!!";
return 0;
}
Examples of C++ iostream
Given below are the examples of C++ iostream:
Example #1
In this example, we are using the ‘cin’ function from iostream to get the value from the user as an input.
Code:
#include <iostream>
using namespace std;
int main()
{
int rollnumber;
cout << "Demo for CIN function in iostream";
cout << "ask user to enter the age here :::";
cin >> rollnumber;
cout << "\nit will print the roll number here " << rollnumber;
return 0;
}
Output:
Example #2
In this example, we are using the ‘cout’ function from iostream to print the user values to the console or screen.
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Demo for COUT function in iostream";
cout << "cout followed by the << operator!!";
cout << "end of the program!!";
return 0;
}
Output:
Example #3
In this example, we are using another function from the iostream header file to handle input and output stream in c++.
Code:
#include <iostream>
using namespace std;
int main()
{
cerr << "This function is used to print the error !!";
return 0;
}
Output:
Example #4
In this example, we are using the error function to trace any error if it occurred; this is also available in the iostream file.
Code:
#include <iostream>
using namespace std;
int main()
{
clog << "This function is used to print the error when occurred in program!!";
return 0;
}
Output:
Conclusion
Iostream provides us with various functions to handle the input and output stream in c++. This iostream header file contains various functions, including cin, cout, cin, and many more. With the help of this, we can read the input, print them, and also trace the error, if any. But to use these functions, we need to include the iostream file in our program in order to work with the functions. For error, they provide us with different functions with some minor changes in each.
Recommended Articles
This is a guide to C++ iostream. Here we discuss How iostream works in C++ and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –