What is the difference between cin and cout streams in c++?


cin is an object of the input stream and is used to take input from input streams like files, console, etc. cout is an object of the output stream that is used to show output. Basically, cin is an input statement while cout is an output statement.

They also use different operators. cin uses the insertion operator( >> ) while cout uses the extraction operator( << ).

For example, if you want to read an int value in a variable my_int(using cin) and then print it to the screen(using cout), you'd write −

Example

#include<iostream>
int main() {
   int my_int;
   std::cin >> my_int;
   std::cout << my_int;
   return 0;
}

Then save this program to hello.cpp file. Finally, navigate to the saved location of this file in the terminal/cmd and compile it using −

$ g++ hello.cpp

Run it using −

$ ./a.out

Output

If you give it the input: 15, this will give the output −

15

Updated on: 11-Feb-2020

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements