Basic Input/Output in C++

Basic Input/Output

In this section, we will learn about the basic input/output commands in C++ using various methods.The input and output in C++ are performed in a sequence of bytes referred to as a stream. There are multiple libraries in C++ which helps us to use the basic input/output functions of c++. We will learn how to input and output basic elements and objects in C++.

basic input/output in c++

Input/Output in C++

In C++ programming language, the two streams can be explained as follows :

Input Stream:
If the direction of flow of bytes is from the device(for example, Keyboard) to the main memory then this process is called input.

Output Stream:
If the direction of flow of bytes is opposite, i.e. from main memory to device( display screen ) then this process is called output.

Header Files for Input/Output in C++

There is a list given below of various header files which help in basic input/output operations in C++:

Header File           Description
iostream          It stands for input/output stream.
iomanip          It stands for input/output manipulators.
fstream          It mainly describes the file stream.
bits/stdc++.h          It includes every standard library.

Basic Input/Output methods in C++

In C++ programming language, some of the input/output methods are :

Standatrd Input Stream – cin command

  • C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard.
  • The extraction operator(>>) is used along with the object cin for reading inputs.

Standatrd Output Stream – cout command

  • The C++ cout statement is the instance of the ostream class and is used to produce output on the standard output device which is usually the display screen.
  • The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator(<<).

Un – buffered standard error stream – cerr command

  • The C++ cerr is the standard error stream that is used to output the errors ands is also an instance of the iostream class. 
  • As cerr in C++ is un-buffered so it is used when one needs to display the error message immediately.
  • It does not have any buffer to store the error message and display it later.

buffered standard error stream – clog command

  • This is also an instance of ostream class and used to display errors but unlike cerr the error is first inserted into a buffer and is stored in the buffer until it is not fully filled. or the buffer is not explicitly flushed (using flush()).

Basic Input/Output commands in C++

Example 1:

Run
#include<bits/stdc++.h> 
using namespace std; 
{
    int roll;
    cout << "Enter your roll number:";     cin >> roll;
    cout << "\nYour roll number is: " << roll;
    return 0;
}

Input:

18

Output:

Enter your roll number:
Your roll number is: 18

Example 2:

Run

#include<bits/stdc++.h> 
using namespace std;
int main()
{
    cerr << "An error occurred";
    return 0;
}

Output:

An error occurred

Example 3:

Run
#include<bits/stdc++.h> 
using namespace std;
int main()
{
    clog << "An error occurred";
    return 0;
}

Output:

An error occurred

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription