Open In App

Basic Input / Output in C++

Last Updated : 26 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

C++ comes with libraries that provide us with many ways for performing input and output. In C++ input and output are performed in the form of a sequence of bytes or more commonly known as streams.

  • 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.

Basic Input / Output in C++

Header files available in C++ for Input/Output operations are: 

  1. iostream: iostream stands for standard input-output stream. This header file contains definitions of objects like cin, cout, cerr, etc.
  2. iomanip: iomanip stands for input-output manipulators. The methods declared in these files are used for manipulating streams. This file contains definitions of setw, setprecision, etc.
  3. fstream: This header file mainly describes the file stream. This header file is used to handle the data being read from a file as input or data being written into the file as output.
  4. bits/stdc++: This header file includes every standard library. In programming contests, using this file is a good idea, when you want to reduce the time wasted in doing chores; especially when your rank is time sensitive. To know more about this header file refer this article.

In C++ after the header files, we often use ‘using namespace std;‘. The reason behind it is that all of the standard library definitions are inside the namespace std. As the library functions are not defined at global scope, so in order to use them we use namespace std. So, that we don’t need to write STD:: at every line (eg. STD::cout etc.). To know more refer this article.

The two instances cout in C++ and cin in C++ of iostream class are used very often for printing outputs and taking inputs respectively. These two are the most basic methods of taking input and printing output in C++. To use cin and cout in C++ one must include the header file iostream in the program.

This article mainly discusses the objects defined in the header file iostream like the cin and cout.  

  • Standard output stream (cout): Usually the standard output device is the display screen. The C++ cout statement is the instance of the ostream class. It 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(<<).

C++




#include <iostream>
 
using namespace std;
 
int main()
{
    char sample[] = "GeeksforGeeks";
 
    cout << sample << " - A computer science portal for geeks";
 
    return 0;
}


Output: 

GeeksforGeeks - A computer science portal for geeks

Time Complexity: O(1)
Auxiliary Space: O(1)

In the above program, the insertion operator(<<) inserts the value of the string variable sample followed by the string “A computer science portal for geeks” in the standard output stream cout which is then displayed on the screen.

  • standard input stream (cin): Usually the input device in a computer is the keyboard. 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. The extraction operator extracts the data from the object cin which is entered using the keyboard.

C++




#include <iostream>
using namespace std;
 
int main()
{
    int age;
 
    cout << "Enter your age:";
    cin >> age;
    cout << "\nYour age is: " << age;
 
    return 0;
}


Input : 

18

Output: 

Enter your age:
Your age is: 18

Time Complexity: O(1)
Auxiliary Space: O(1)

The above program asks the user to input the age. The object cin is connected to the input device. The age entered by the user is extracted from cin using the extraction operator(>>) and the extracted data is then stored in the variable age present on the right side of the extraction operator.

  • Un-buffered standard error stream (cerr): The C++ cerr is the standard error stream that is used to output the errors. This 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.
  • The main difference between cerr and cout comes when you would like to redirect output using “cout” that gets redirected to file if you use “cerr” the error doesn’t get stored in file.(This is what un-buffered means ..It cant store the message)

C++




#include <iostream>
 
using namespace std;
 
int main()
{
    cerr << "An error occurred";
    return 0;
}


Output: 

An error occurred

Time Complexity: O(1)
Auxiliary Space: O(1)

  • buffered standard error stream (clog): 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()). The error message will be displayed on the screen too.

C++




#include <iostream>
 
using namespace std;
 
int main()
{
    clog << "An error occurred";
 
    return 0;
}


Output: 

An error occurred

Time Complexity: O(1)
Auxiliary Space: O(1)

 Related Articles:  


 



Previous Article
Next Article

Similar Reads

Basics of Input and Output in Windows PowerShell
Windows PowerShell is a command-line and scripting language designed for system administration. It helps IT professionals, to control and automate the administration of the Windows Operating System and Windows Server Environment. There are cmdlet for console input and output using PowerShell. Example 1: Showing the Output to console Function to pri
2 min read
Java IO : Input-output in Java with Examples
Java brings various Streams with its I/O package that helps the user to perform all the input-output operations. These streams support all the types of objects, data-types, characters, files etc to fully execute the I/O operations. Before exploring various input and output streams lets look at 3 standard or default streams that Java has to provide
7 min read
Cascading of Input/Output Operators in C++
Prerequisite: Operator Overloading in C++, Types of Operator Overloading When an object calls an operator function by passing an argument and the returned value of the operator function calls the next operator function in the same expression, it is called as cascading of operators. Below are the examples for illustration of Cascading Of Operations:
5 min read
Unformatted input/output operations In C++
In this article, we will discuss the unformatted Input/Output operations In C++. Using objects cin and cout for the input and the output of data of various types is possible because of overloading of operator &gt;&gt; and &lt;&lt; to recognize all the basic C++ types. The operator &gt;&gt; is overloaded in the istream class and operator &lt;&lt; is
4 min read
Input/Output Operators Overloading in C++
Operator Overloading is a part of Polymorphism, which enables the feature because of which we can directly use operators with user-defined classes and objects. To read more about this, refer to the article operator overloading in C++. Input/Output Operators(&gt;&gt;/&lt;&lt;) Overloading in C++ We can't directly use the Input/Output Operators (&gt;
2 min read
How to Output Error When Input Isn't a Number in C++?
In C++, when taking user input, we may need to validate that it is a number and output an error if it is not. In this article, we will learn how to output an error when the input isn't a number in C++. For Example, Input:Enter a Number: GOutput:Error: That was not a number.Output Error When Input Isn't a Number in C++The std::cin automatically dedu
2 min read
How to Ask User Input Until Correct Input is Received?
User input is a common source of errors in C++ programs so it’s important to validate user input to make the program more reliable. In this article, we will discuss how to ask the user for input until valid input is received in our C++ program. For Example, Input: asdf Output: Incorrect Input. Please Enter Valid Input.User Input ValidationThe easie
2 min read
Output of C++ Program | Set 10
Predict the output of following C++ programs.Question 1 C/C++ Code #include&lt;iostream&gt; #include&lt;string.h&gt; using namespace std; class String { char *p; int len; public: String(const char *a); }; String::String(const char *a) { int length = strlen(a); p = new char[length +1]; strcpy(p, a); cout &lt;&lt; &quot;Constructor Called &quot; &lt;
2 min read
Output of Java Programs | Set 54 (Vectors)
Prerequisite : Vectors in Java Basics 1. What is the Output Of the following Program Java Code import java.util.*; class demo1 { public static void main(String[] args) { Vector v = new Vector(20); System.out.println(v.capacity()); System.out.println(v.size()); } } Output: 20 0 Explanation: function - int capacity( ) Returns the capacity of the vect
6 min read
Output of C++ Programs | Set 49
What is the output of the following program? C/C++ Code #include &lt;iostream&gt; using std::cout; int main() { int i = 0; cout &lt;&lt; (i = 0 ? 1 : 2 ? 3 : 4); return 0; } a. 1 b. 2 c. 3 d. 4 e. compile error Answer : C Explanation: Ternary operator is right to left associative. So the expression becomes (i = 0 ? 1 : (2 ? 3 : 4)) which evaluates
3 min read