What Does “<<” and “>>” Mean in C++ for cout/cin
The stream insertion operator “<<“ and stream extraction operator are the bitwise shift operators used in C++ programming. The “<<” is used with cout for displaying the output, while “>>” is used to create a program that is user-oriented. You feed input into the program using cin, while the output is displayed through cout.
Let’s give you an idea about these operators through a simple example C++ example, which is shown below:
First insert the C++ <iostream> and namespace using the below-mentioned syntax:
using namespace std;
Then start the main function. In programming languages; like C++, the main() function is the function which has all the executable code enclosed in it:
return 0;
}
1: Using “Cout <<”
Now finally it is time to use the cout command. In C++, the cout command is used to get the output of the code on the output screen. The “<<” symbol is used with the cout command to indicate that whatever comes after “<<” is required as an output. For instance, in the below-stated example, the string “Welcome to LinuxHint!” is required as an output. So, the string is written after “<<” symbol:
Program 1
using namespace std;
int main() {
cout<<"Welcome to LinuxHint!";
return 0;
}
Output
2: Using “Cin >>”
After cout, now let’s try to understand how to use “>>” with cin. We use cin in a code to get the input from the user and with cin, the “>>” symbol is used to determine that input is required by the user. In the below-written example, I wanted to create a user-defined program where every user can enter his/her name. So, after defining a variable “name” I have used a cin>> to get a user-defined input for the “name” variable:
Program 2
using namespace std;
int main() {
string name;
cout<> name;
cout<< "Your Name Is: "<< name;
return 0;
}
On the output screen you can see that the first statement is displayed using cout, which is asking a user to enter the name:
Then the cin >> command will run and the user has to enter a string of name:
Here, I have added a string LinuxHint on as an input (on the output screen):
Now to display the name that the user has entered as an input, use the cout << command. Here you can see that “<<” symbol is used twice to separate the output string(“Your Name Is: “) and name (the variable which has user-defined input stored in it):
Now in another example, let’s create a user-defined addition program where a user can enter the input of his/her choice and can add them. Just like the previous example, we will follow the same C++ syntax where <iostream> library is imported first, and then all the programs will be written inside the main function:
Program 3
In the below-written C++ program, there are two integer type variables x and y. The user can add any values for x and y and as an output, the program will give the sum of both the x and y. To perform this, we will be required to use cout<< and cin>> commands, all the inputs that are required by the user will be written after cin>> and to display the output on the screen cout<< will be used:
using namespace std;
int main() {
int x, y;
cout<> x;
cout<> y;
cout<< "Sum =" << x+y;
return 0;
}
On the output screen you can see that I have entered the first number as 5 then second number 6 and the sum of these numbers is displayed as an output:
Similarly, by using cin<< and cout>>, many different programs can be created in C++. Just remember that << is used with cout to determine the output and >> is used with cin to determine the user-defined input.
Conclusion
In C++ language, the syntax is such that; cout is used to display the output and cin is used to get the input from the user. “<<” is used in C++ with cout to determine that whatever is written after “<<” is required as an output on the output screen. Whereas, “>>” is used with cin to differentiate and state that the user is required to enter the input here. Both << and >> are part of C++ syntax.