There are various ways for performing input and output in C++. In C++, an input/output is defined as a way of storing and retrieving the data in a variable, during the execution of the program. Have you ever noticed that, when you enter a value, how the compiler reads it and also when displaying the output while execution. Well, all this happens due to stream.
A stream is defined as a sequence of bytes which is used for performing input and output while execution of 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 flows from 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 pre-defined function known as cin.
- Output Stream: Here the direction is reverse, that is, from main memory (RAM) to the output devices (mainly Monitor or any display screen), this process is known as output.
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 consist 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 that, while using cin always use >> “extraction operator”. If you don’t know what is an operator, then checkout this link on DigitalGyan, https://digitalgyan.org/what-are-operators-in-c/
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 screen. Don’t forget to use insertion operator (<<) while using cout function.
Practical use of Input and Output in C++
Open up you IDE and try executing this command and see the output by yourself. If you don’t have and IDE, check this link : https://digitalgyan.org/setting-up-c-environment/
Using namespace std;
cout<<”Enter a number”;
cout<<”The number is “<<n;