In C++, input and output (I/O) operators are used to take input and display output. The operator used for taking the input is known as the extraction or get from operator (>>), while the operator used for displaying the output is known as the insertion or put to operator (<<).
We’ll be covering the following topics in this tutorial:
Input Operator
The input operator, commonly known as the extraction operator (>>), is used with the standard input stream, cin. As stated earlier, cin treats data as a stream of characters. These characters flow from cin to the program through the input operator. The input operator works on two operands, namely, the c in stream on its left and a variable on its right. Thus, the input operator takes (extracts) the value through cin and stores it in the variable.
To understand the concept of an input operator, consider this example.
A program to demonstrate the working of an input operator.
#include using namespace, std; int main () { int a; cin>>a; a = a+1; return 0; }
In this example, the statement cin>> a takes an input from the user and stores it in the variable a.
Output Operator
The output operator, commonly known as the insertion operator (<<), is used. The standard output stream cout Like cin, cout also treats data as a stream of characters. These characters flow from the program to cout through the output operator. The output operator works on two operands, namely, the cout stream on its left and the expression to be displayed on its right. The output operator directs (inserts) the value to cout.
To understand the concept of output operator, consider this example.
A program to demonstrate the working of an output operator.
#include using namespace std; int main () { int a; cin>>a; a=a+1; cout<<a; return 0; }
This example is similar to Example 1. The only difference is that the value of the variable a is displayed through the instruction cout << a .
Cascading of Input/Output Operators
The cascading of the input and output operators refers to the consecutive occurrence of input or output operators in a single statement.
To understand the concept of cascading of the input/output operator, consider these examples.
A program without cascading of the input/output operator.
#include using namespace std; int main () { int a, b; cin>>a; cin>>b; cout<<"The value of a is"; cout<<a; cout<<"The value of b is"; cout<<b; return 0; }
In this example, all cin and cout statements use separate input and output operators respectively However, these statements can be combined by cascading the input and output operators accordingly as shown in this example.
A program with cascading of the input/output operator
#include using namespace std; int main () { int a, b; cin>>a>>b; cout<<"The value of b is : "<<b; cout<<"The value of a is "<<a; return 0; }
In this example, the cascaded input operators wait for the user to input two values and the cascaded output operator first displays the message The value of a is: and then displays the value stored in a. Similar is the case for the next statement.
It can be observed that cascading of the input/output operator improves the readability and reduces the size of the program.