Methods to Perform Input and Output
We mainly use two instances of iostream for printing outputs and taking inputs from the user.
- We use cin for taking inputs.
- We use cout for printing outputs.
One must include the iostream header file in their program to use these two instances.
cin (Standard Input Stream)
Commonly, we use the keyboard as an input device in our computers. cin statement is the instance that comes under the ostream header class and is used to take input from the input device, a keyboard, in most cases.
The extraction operator(>>) is used with the cin for taking inputs. The primary function of the extraction operator(>>) is to extract data from the object cin, which is fed using the keyboard.
You can also do a free certification of Basics of C++.
Below is the code for the syntax of the cin object with a code.
CODE:
#include <iostream> // Including iostream header file to use cin object
using namespace std;
int main()
{
int N; // Declaring a variable which is to be taken as input
cin >> N; // Taking N as input
cout <<” Output is: ” N; // Printing N on-screen
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
INPUT
6
OUTPUT
Output is: 6
The above program asks for input from the user. The integer entered by the user is extracted from cin using the extraction operator(>>), and the data extracted is then stored in the variable N present on the right side of the extraction operator.
cout (Standard Output Stream)
A standard output device used in the computer is the display screen. cout statement is the instance of the istream class. It is then used to produce output on the output device, usually the display screen. The data displayed on the screen is inserted in the standard output stream (cout) using the insertion operator(<<).
CODE
#include <iostream> // Including iostream header file to use cin object
using namespace std;
int main()
{ char N[]="Ninjas!"; // Initializing variable N with string Ninjas
cout <<"Coding "<<N; // Printing N on-screen
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
OUTPUT
Coding Ninjas!
In the above program, the value of the string variable is inserted into the standard output stream cout using the (<<) insertion operator.
You can try and compile with the help of online c++ compiler.
cerr (Unbuffered Standard Stream)
cerr is used to print errors or output errors. cerr returns an ostream object. Since it is un-buffered, it only outputs error messages.
The primary difference between cout and cerr is that cout is primarily used for printing out the program’s output, whereas cerr should show all the information and messages of error.
Below is the code to show an example of cerr
CODE
#include <iostream>
using namespace std;
int main() {
cerr << "Error is found!!"; // printing the error message on screen
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
clog (Buffered Standard Error stream)
clog is used to show error messages like cerr, but it differs in a way as it first loads the error into the buffer until it is entirely complete or flushed explicitly.
It is also an instance of ostream class.
CODE:
#include <iostream>
using namespace std;
int main()
{
clog << "An error is there"; //Printing error message
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
OUTPUT:
An error is there
Also Read - C++ Interview Questions
Comparison Between Buffered and Unbuffered Output Stream
Buffered output is considered to be more structured than unbuffered output. In the buffered output(clog), all the output is first saved into a variable and then written to the disk at once. Whereas in unbuffered output, we have to keep writing to disk continuously.
Buffered output isn't really preferred for significant errors. In case of a system crash, a situation could arise where the output was still in the buffer but wasn't written to disk, and the buffered output couldn't retrieve the error message. In contrast, the unbuffered output will keep writing to the disk so it won't lose any error message, although it is a bit slow.
Also read - File Handling in CPP
FAQs
-
What is the namespace of cout and cin in C++?
The namespace of cout and cin is std in C++.
-
What do we understand by >> and << operators?
>> and << when used in general, they act as shift operators, whereas when we use >> with cin, it acts as an extraction operator. Similarly, when we use << with cout, it acts as an insertion operator.
-
Can we use printf and scanf in place of cout and cin, respectively, in C++?
Yes, we can use printf and scanf in C++ using the cstdio header file because all the functionalities of C are already present in C++.
Key Takeaways
In this blog, we learned how to perform input and output operations in C++ using various header files. Then we learned about different header files which contain methods to do so.
After that, we went through all such methods and also saw their application using code.
If you want to learn more about various fundamental concepts of programming, then you can learn here.