Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Header Files To Perform Input And Output
3.
Methods to Perform Input and Output
3.1.
cin (Standard Input Stream)
3.2.
cout (Standard Output Stream)
3.3.
cerr (Unbuffered Standard Stream) 
3.4.
clog (Buffered Standard Error stream)
4.
Comparison Between Buffered and Unbuffered Output Stream
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Input/Output Streams

Author Vivek Goswami
4 upvotes
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

This blog will teach us to perform input and output operations in C++.

C++ has inbuilt libraries that provide us with the functionalities that help perform input and output operations.

C++ performs input and output operations in the form of streams.

Streams are a sequence of bytes or data flow into or out of the program.

Streams are  of two types:

  1.  When the data flow is into the main memory using any device (e.g., keyboard), it is called Input Stream. 
  2.  When the data flow is from the main memory to any device (e.g., Laptop Screen), it is called Output Stream. 


Also, see Literals in C, Abstract Data Types in C++

Header Files To Perform Input And Output

  1. iostream: iostream is the acronym for standard input-output stream. This header file is mainly used for performing input and output operations, and it contains definitions of objects like cin, cout, cerr, etc.
     
  2. iomanip: iomanip is the acronym for input-output manipulators. The methods declared in this header file are used for manipulating input and output streams. This file contains definitions of various objects like setw, setprecision, etc.
     
  3. fstream: This header file’s primary purpose is to describe the file stream. This header file handles the data taken as input by reading and writing in a file. This file contains objects like is_open, close, etc.

Read More About, C++ Basics

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Methods to Perform Input and Output

We mainly use two instances of iostream for printing outputs and taking inputs from the user.

  1. We use cin for taking inputs.
  2. 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;
}


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;
}


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;
}

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;
}

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

  1. What is the namespace of cout and cin in C++?
    The namespace of cout and cin is std in C++.
     
  2. 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.
     
  3. 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.

Previous article
Setw Cpp
Next article
File Handling in Cpp
Live masterclass