Input/Output Operators in C++
In C language we are using the printf function for output on the screen and scanf function for take input from the user. In C++ we have we are using cout for output on screen and cin for input from the keyboard.
Output in C++ using cout:
In C++ the cout is used for printing the message on the screen. Consider the below statement.
cout << "Hello ISC";
The above statement simply prints the message Hello ISC on the monitor screen. This statement contains two features of C++ that is cout and << . The cout is considered as an identifier we pronounced it as "C out" which is predefined object in C++ that represents the
Standard Output Stream in C++. The standard output stream is screen. We can also send the output to the other output stream such as File but the standard output stream is screen.
The << operator is known as Insertion or Put to Operator.. It inserts the variable value on the screen. We can refer << as a Bitwise Left Shift operator and still it can used for this purpose. Here you can see the same operator << is used for output purpose. This is called Operator Overloading in which the same operator used for different purpose based on the operands used with it.
The below example shows the various use of cout:
cout << "Hello ISC" //simple use to print the string
cout << "Value of a=" << a; //To print the value of variable a
cout << "Value of a=" << a << "\n Value of b=" << b; //print multiple values
Input in C++ using cin:
In C++ the cin is used for take input from the keyboard. Consider the below statement.
cin >> a;
The above statement reads the value from the keyboard and store it in variable a. In C language we need to specify the data type of the variable which we are going to read but in C++ we need to only give the name of the variable. All data type related work is handled by compiler, no need to worry about specifying data type. We can consider it as one of the advantages of C++ over C language. The statement "cin>>a; " introduces the two new features of C++ that is cin and >> .
The cin is considered as an identifier in C++ which is pronounced as "C in". The cin is predefined object provided by C++ that is connected with the Standard Input Stream. The standard input stream is keyboard.
The operator >> is known as extraction get from operator which extracts the or we can say takes the value from the keyboard and assigns it to the variable which is on its right side. As we discussed in << operator, the >> operator also used for Bitwise Right Shift.
The below example shows the various use of cin:
cout << "Enter value of a:" ;
cin >> a ; //reads the value of a from keyboard
cout << "Value of a=" << a;
cout << "Enter value of a and b:";
cin >> a >> b; //reads the value of a and b
cout << "Value of a=" << a << "\n Value of b=" << b ;
Cascading of I/O operators:
The multiple use of << or >> in a one statement is known of cascding.
Cascading of output operator ( >>):
cout << " Hello " << " ISC ";
cout << "Value of B=" << b;
Cascading of input operator (>>):
cin >> n1 >> n2 >> n3 ;
cin >> n1 >> n2 ;
Input/Output Operators in C++