
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
15
Standard Output Stream - cout
The cout is stands for character output, which is an object of the ostream class (iostream means input/output stream), which is used to display output to the device, that's basically a screen. Here, the bytes move from the computer's memory to a device (like the screen).
This is done using the insertion operator (<<), which helps to insert data into the output stream (cout). This can be formatted using manipulators and flushed with endl or flush.
Syntax
Here is the following syntax for it.
cout << variable_name;
Example
#include <iostream> using namespace std; int main() { cout<<"Hello! TutorialsPoint Learner!"; return 0; }
Output
Hello! Tutorialspoint Learner!
Standard Input Stream - cin
The cin in C++ stands for character input. It's an object of the istream class (iostream means input/output stream), which is used to receive input from the standard input device, that's basically the keyboard. Here, the bytes move from a device to the computer's memory
This is done using the extraction operator (>>), which helps to extract the input from the cin and stores it in a variable. It automatically skips the whitespaces (spaces, tabs, newlines) until a special method like getline() is used.
Syntax
Here is the following syntax for it.
cin >> variable_name;
Example
#include <iostream> using namespace std; int main() { string name; cout<<"Enter your name:"; cin>>name; cout<<"Hello "<< name << " Welcome to TutorialsPoint"; return 0; }
Output
Let's assume the user has given input Rahul, therefore the following output will be;
Enter your name: Rahul Hello Rahul Welcome to TutorialsPoint