Programming

Input and Output in C++ | Free C++ Ebook PDF Download

In the programming language C++, input/output is accomplished via streams. A stream is an abstraction that represents a device on which input and output operations can be performed.

There are three standard streams defined by the C++ standard library, which are attached to the computer’s standard input, output, and error devices:

Have you ever noticed how the compiler reads it when entering a value and displaying the output during execution? Well, all this happens due to stream.

input and output in c++
Input and Output in C++ | Free C++ Ebook PDF Download 1

Stream

A stream is defined as a sequence of bytes used to perform input and output while executing the program. In simple words, it is the flow of data in bytecode. These are predefined in the header files like iostream. These are of two types: input stream and output stream.

  • Input Stream: In this type of stream, the data flow from the device (keyboard or other input devices) to the memory (RAM) of the computer and this process is known as Input in C++. It is achieved through a pre-defined function known as cin.
  • Output Stream: Here, the direction is reversed, that is, from the main memory (RAM) to the output devices (mainly Monitor or any display screen); this process is known as output.
input and output in c++ | free c++ ebook pdf download
Input and Output in C++ | Free C++ Ebook PDF Download 2

Some of the header files that are used in C++ are:

  • iostream : This is the standard header for input and output in C++. Hence you see this at the top of the program. Syntax :-  #include<iostream> .  It consists of predefined cin, cout, cerr, clog, etc. functions.
  • iomanip : Iomanip stands for (Input and Output + Manipulation) or input and output with manipulation. The methods are used for manipulating the input and output streams. Example setw, setprecision, etc.
  • Fstream : This header file mainly describe the flow of data to be either written or extracted from a file. It handles the flow of stream with the input/output operation from the file while executing the program.

Standard Input in C++

In C++, the standard input is done through cin predefined function. You can also go with scanf() in C++. One important thing to note is that, while using cin, always use >> “extraction operator”.

Standard Output in C++

In C++, the standard output is done through cout predefined function. It is an instance or object in the predefined ostream class. The cout function displays output on the screen. Don’t forget to use the insertion operator (<<) while using cout function.  

Practical use of Input and Output in C++

Open up your IDE, try executing this command, and see the output by yourself.

#include<iostream>
Using namespace std;
int main()
{
   int n;
   cout<<”Enter a number”;
   cin>>n;
   cout<<”The number is “<<n;
    return 0;
}

If you wish to learn more about inputs and outputs or anything else about C++, you can download this ebook on C++ programming by Jeremy A. Hansen.

Leave a Reply

Back to top button