C++ Input Output | Basics of cin and cout in C++

C++ Input Output | Basics of cin and cout in C++

4 mins read3.4K Views Comment
clickHere
Atul
Atul Harsha
Senior Manager Content
Updated on Sep 22, 2023 18:41 IST

Do you know how a computer gets information from you or gives you information? Just like C has scanf and printf, in C++ we use ‘cin’ and ‘cout’. ‘Cin’ takes the input and helps the computer understand what you want to tell it. ‘Cout’ helps the computer tell you something by generating the output. It’s like having a conversation with the computer, isn’t it cool? Let’s learn more about it together!”

2023_01_Basics-of-cin-cout-in-C-1.jpg

Basic input and output operations are an essential part of any programming language, and C++ is no exception. In C++, input and output operations are performed using streams, which are sequences of bytes that can be read from or written to. The C++ standard library provides several classes and functions for working with streams The two most commonly used streams for basic input and output are cin and cout in C++.

Read More: What is C++

Table of Content

What is Cin and Cout in C++?

  • cin and cout are standard input and output streams in C++.
  • cin: Read input from the user.
  • cout: Write output to the screen.

For example, to read an integer from the user and store it in a variable x, you can use the following code:

 
int x;
cin >>"Give your input: ">> x;
Copy code

And to output a message to the screen, you can use the following code:

 
cout <<"Hey there, its me";
Copy code

It is also common to use the << operator to chain multiple output statements together, like this:

 
cout <<"The value of x is: "<< x;
Copy code

Here’s another example to understand it further:

 
#include <iostream>
using namespace std;
int main()
{
int age;
string name;
cout << "What is your name? ";
cin >> name;
cout << "What is your age? ";
cin >> age;
cout << "Hello, " << name << "! You are " << age << " years young." << endl;
return 0;
}
Copy code

Output:

What is your name?
Jim
What is your age?
18
Hello, Jim ! You are 18 years young.

Read More in C++:

Classes and Objects in C++
Classes and Objects in C++
If you are a student then you must have got this question in the exam or practical viva and if you are a job seeker then you must have faced...read more
Difference Between C and C++
Difference Between C and C++
C is a procedural programming language, focusing on the sequence of actions to solve a problem. It’s about functions and structured programming. In contrast, C++ is an extension of C...read more
All About OOPs Concepts in C++
All About OOPs Concepts in C++
Have you ever wondered how complex software systems are so efficiently organized and managed? This is where the principles of Object-Oriented Programming (OOP) in C++ come into play. OOP in...read more

What are Stream? Different type of streams in C++

A stream is a sequence of bytes that can be read from or written to in C++. Streams are used to perform input and output operations. Here are some different types of streams in C++:

Stream Description
cin The standard input stream, used to read input from the keyboard.
cout The standard output stream, used to write output to the screen.
cerr The standard error stream, used to write error messages to the screen.
clog The standard logging stream, used to write logging messages to the screen.
ifstream The input file stream, used to read data from a file.
ofstream The output file stream, used to write data to a file.
fstream The file stream, used to read and write data to a file.

All these streams are inherited from iostream class. They are also known as stream classes. They provide the basic functionality for input and output operations and are typically used in conjunction with other classes and functions to perform more advanced operations.

Must Check: Difference Between C and C++

What Header files are available in C++ for Input/Output operations?

We just learned about streams in C++ which is used for input and output operations. The standard library provides several header files that contain the necessary classes and functions for working with these streams. Here are some of the header files available in C++ for input and output operations:

Header File Description
<iostream> The main header file for input and output operations in C++. It contains the cin, cout, cerr, and clog streams.
<fstream> The header file for working with file streams. It contains the ifstream, ofstream, and fstream classes.
<stream> The header file for working with string streams. It contains the istringstream, ostringstream, and stringstream classes.
<iomanip> The header file for working with input and output manipulators. It contains functions like setw and setprecision that can be used to format output.

It’s important to note that these are the basic header files and C++ has more libraries that can be used for input and output operations like for working with network streams, serial communication, and etc.

What is the difference between cin and scanf ?

Here’s a comparison of the key differences between cin and scanf in C++:

cin scanf
cin is a part of the C++ standard library and is used to read input in C++. scanf is a part of the C standard library and is used to read input in C.
It uses the >> operator for input. It uses format specifiers for input.
Automatically handles type conversion and formatting. Requires manual type conversion and formatting.
cin has additional functionality like error handling, which makes it easier to handle invalid input. scanf has fewer functionalities compared to cin.
It can be used to read input from a file by tieing it to ifstream and fstream. Used to read input from a file by redirecting standard input.

What is the difference between cout and printf?

Cout is more powerful and convenient than printf in C++ due to its type-safety and additional functionalities. Let’s see a comparison between them:

cout printf
cout is a part of the C++ standard library and is used to write output in C++. printf is a part of the C standard library and is used to write output in C.
Uses the << operator for output. Uses format specifiers for output.
Automatically handles type conversion and formatting. Requires manual type conversion and formatting.
Supports additional functionality like error handling, which makes it easier to handle invalid input. Has fewer functionalities compared to cout.

NOTE: scanf and printf is widely used in C programming but can also be used in C++ for input and output. It recommended to use cin-cout for C++.

About the Author
author-image
Atul Harsha
Senior Manager Content

Experienced AI and Machine Learning content creator with a passion for using data to solve real-world challenges. I specialize in Python, SQL, NLP, and Data Visualization. My goal is to make data science engaging an... Read Full Bio