Input Output Operator (<<) (>>)

The shift operators (>> and <<) have been overloaded so you can perform I/O operations on istream and ostream objects, most notably cout, cin, and file streams. Thus you could just do console I/O operations.

In C++ input operator(>>) and output operator(<<) for console IO operations are explained below.

1. Output Operator (Insertion Operator <<)

To display single line or statement on console IO window “cout<<” function is used. cout<< uses to manage output streams on console IO window. cout<< is a library function defined in <iostream.h> header file of c++ library.

Example:

cout <<”Hello, World”;

This statement simply prints “Hello World ” on output window. To print more than one statement on console window :

Example:

cout<< "This is just like a printf statement in c"<< endl;
cout<<"This is statement 1" << "This is statement2" ;

Example:

int x=10 ;
cout<<" value of x="<< x;

Here, in above statement we have used cout<< function to display the variable value on console window. You can print any number of variable value to the console window. Just like this:

Example:

int x=10;
int y=20;
cout<<"Value for X=" <<x;
cout<<"Value for Y=" <<y;

2. Input Operator (Extraction Operator >>)

To read a value or any input from the user using console IO ,input operator “cin<<” is available from the c++ library <iostream.h>. cin<< is input library function with extraction operator to manage user input. cin<< will read a used input and assigns the value to the variable.

Example:

1. cin >> number1;
2. cin >> valueX >> valueY;

here, line 1 will read single user input for variable number1 and line 2 will read user input twice for two variables valueX and valueY.

Example:

#include<iostream.h>
void main()
{
int x;
cout<<"Enter value for X";
cin>>x;
cout<<"\n Value of X="<< x;
}

The shift operators (>> and <<) have been overloaded so you can perform I/O operations on istream and ostream objects, most notably cout, cin, and file streams. Thus you could just do console I/O operation.

Related posts