Home » C++ programming language

Basic Input & Output using cin and cout



cout [console output] <<

cout<< is used to display message/ value of variable to the console output device.

cout is an object of ostream class, operator << is known as insertaion or put to operator. it sends the value to the output device.

Simple form of cout

    cout<<"Message to display";
    cout<<variable_name;

Cascading form of cout

    cout<<"Message to display"/variable_name <<"Message to display"/variable_name<< ... ;

Consider the example

// Program to illustrate cout 
// Author : includehelp.com
// Date : 13-03-2012

#include <iostream.h>

int main()
{
	char text[]="Hello Guys!";
	int  val=100;
	cout<<"welcome to includehelp.com \n";
	// Using cascading form of cout
	cout <<"text:"<<text <<", val:"<<val;
	
	return 0;
}

Output

    welcome to includehelp.com
    text: Hello Guys! ,val:100
  • cout is a static object of ostream class.
  • while << (insertion operator)/(put to operator) is an overloaded operator of it.
  • you can also write cout.operator<<("includehelp.com"); instead of cout<<"includehelp.com";


cin [console input] >>

cin>> is used to take value of variable from input device (keyboard).

cin is an object of istream class, operator >> is known as extraction or get from operator. it extracts (takes) the value from keyboard.

Simple form of cin

    cin>>variable_name1>>variable_name2>>...;

Cascading form of cin

    cin>>variable_name1>>variable_name2>>...;

Consider the example

// Program to illustrate cout 
// Author : includehelp.com
// Date : 13-03-2012

#include <iostream.h>

int main()
{
	char text[]="Hello Guys!";
	int  val=100;
	cout<<"welcome to includehelp.com \n";
	// Using cascading form of cout
	cout <<"text:"<<text <<", val:"<<val;
	
	return 0;
}

Output

    Enter your name : Mike
    Enter your marks: 97
    Hello Mike , You got 97 marks in exam.

Consider the example - Using cascading form of cin

/* Program to illustrate cin 
   Author : includehelp.com
   Date : 13-03-2012
*/

#include <iostream.h>

int main()
{
	char name[30]; 
	int  marks;

	cout<<"Enter you name and marks : ";
	cin>>name>>marks ;
	cout <<"Hello "<<name<<" , You got "<<marks<<" marks in exam.\n";

	return 0;
}

Output

    Enter your name : Mike
    Enter your marks: 97
    Hello Mike , You got 97 marks in exam.
  • cin is a static object of istream class.
  • while >> (extraction operator)/(get from operator) is an overloaded operator of it.
  • you can also write cin.operator>>(variable_name); instead of cin>>variable_name;





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.