Resources » Articles/Knowledge Sharing » Computer & Technology

Input/Output Operators in C++


Posted Date: 05-Mar-2011  Last Updated:   Category: Computer & Technology    
Author: Member Level: Gold    Points: 15


In C programming language we are using printf and scanf for input/output purpose, but C++ introduced the new features of I/O operations that is cout and cin in which programmer no need to worry about specifying data type of the variable.



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:



#include

int main()
{
int a=5,b=30;

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.


int a;
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:



#include

int main()
{
int a,b;

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 (>>):



int n1,n2,n3;
cin >> n1 >> n2 >> n3 ;
cin >> n1 >> n2 ;


Did you like this resource? Share it with your friends and show your love!




Responses to "Input/Output Operators in C++"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Awards & Gifts
    Active Members
    TodayLast 7 Daysmore...

    Online Members

    K Mohan
    More...
    ISC Technologies, Kochi - India. Copyright © All Rights Reserved.