Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is #include <iostream> in C++?
2.1.
Syntax of <iostream>
2.2.
1. Standard Input Stream -- cin
2.3.
HTML
2.4.
2. Standard Output Stream -- cout
2.5.
C++
3.
How <iostream> Works in C++?
3.1.
1. Input Stream
3.2.
2. Output Stream
4.
Different Operations in <iostream>
4.1.
1. cin (Standard input stream in C++)
4.2.
C++
4.3.
2. cout (Standard output stream in C++)
4.4.
C++
4.5.
3. cerr (Standard error stream in C++)
4.6.
C++
4.7.
4. clog (Standard logging stream in C++)
4.8.
C++
5.
Global Stream Objects of C++
5.1.
Narrow Characters
5.2.
Wide Characters
5.2.1.
wcin
5.3.
C++
5.3.1.
wcout
5.4.
C++
5.4.1.
wcerr
5.5.
C++
5.5.1.
wclog
5.6.
C++
6.
More Examples of <iostream>
6.1.
1. Using Classic <iostream>
6.2.
C++
6.3.
2. Using Standard <iostream>
6.4.
C++
6.5.
​​3. Forward Declaration with Standard <iostream>
6.6.
C++
6.7.
4. Forward Declaration with Classic <iostream>
6.8.
C++
6.9.
5. Below is the example code for Both Classic and Standard <iostreams>
6.10.
C++
7.
Why Should We Use <iostream> Instead of the Traditional <cstdio>?
7.1.
1. Type-Safety
7.2.
2. Error Prone
7.3.
3. Extensible
7.4.
4. Inheritable
8.
Frequently Asked Questions
8.1.
Why do we use #include iostream in C++?
8.2.
Is #include iostream a function?
8.3.
What is the use of #include iostream in C?
8.4.
What is header file in C++?
9.
Conclusion 
Last Updated: Apr 11, 2024
Medium

What is #include <iostream> in C++?

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

#Include <iostream> stands for standard input-output stream. It declares objects that control reading from and writing to the standard streams. Today we will learn about the header, which helps us to perform input-output operations, and its importance for these operations in C++. This will help you understand the input-output operations, how these operations work in <iostream>, and what header files are in C++. 

include iostream

This article will look at the include iostream header file in C++. We will discuss what it does and allows us to do in our programs and how to have it. 

What is #include <iostream> in C++?

iostream stands for standard input-output stream. #include <iostream> is a keyword used to include the iostream header file. The iostream header file contains all the functions used for reading and writing operations like cin and cout.

We may use C++ functions that allow us to read and write text on the computer screen, similar to a console or terminal, by including the include iostream header file in our program. To write programs that interact with users, we must be able to receive input from the user and display output on the screen.

Syntax of <iostream>

#include <iostream>

 

This statement is placed at the beginning of the C++ program, before the "main" function, to make the "iostream" library available for use in the program. 

Streams are an essential part of the C++ input/output library and provide a convenient and flexible way to handle input and output operations in C++ programs.

In C++, the iostream library provides two types of streams: input streams and output streams.

1. Standard Input Stream -- cin

Input streams in C++ are used to read data from a source, such as a keyboard, a file, or a network connection. The standard input stream "cin" reads data from the keyboard.

Syntax: 

“cin”


Example:

  • HTML

HTML

#include <iostream>
using namespace std;
int main()
{
   int age;
   cout << "Please enter your age: ";
   cin >> age;
   cout << "Your age is: " << age <<endl;
   return 0;
}


In this example, the "cin" function reads an integer value entered by the user and stores it in the "age" variable.

Output:

Please enter your age: 27
Your age is: 27

2. Standard Output Stream -- cout

Output streams in C++ are used to write data to a destination, such as a screen, a file, or a network connection. The standard output stream "cout" writes data to the screen.

Syntax: 

“cout”


Example:

  • C++

C++

#include <iostream>
using namespace std;
int main()
{
   cout<<"Hey Ninjas!!";
   return 0;
}


In this example, the "cout" function displays the message "Hey, Ninjas!!" on the console or terminal. 
 

Output:

Hey Ninjas!!
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

How <iostream> Works in C++?

Iostream uses the objects like cin, and cout to take input from the user and to display the out on the screens.

Now, let us have a look at input and output streams separately.

1. Input Stream

  • This stream helps in the input processes i.e. to take the data from the device via keyboard and store it in the main memory of the system.
     
  • An input stream such as cin is the source of data that can be read onto the variables.
     
  • Istream, ifstream, and istringstream are the three main input streams.
     
  • sequential text-mode input is done by istream, while ifstream and istringstream support disk file input and input from in-memory strings, respectively.

2. Output Stream

  • This stream helps in the output processes, i.e., it takes data from the main memory and gives it to the device. This is the opposite of the input stream.
     
  • As input stream was a source of data, but the output stream is a destination for the data.
     
  • Ostream, ofstream, and ostringstream are the main output streams.
     
  • Predefined objects are used by ostream, while ofstream and ostringstream support disk file output and output from in-memory strings, respectively.

Different Operations in <iostream>

In this discussion, we can explore the different operations that can be performed using iostream and how they can be used to perform input and output operations in C++.

1. cin (Standard input stream in C++)

"cin" is a tool in C++ that allows you to take input from the user (usually from the keyboard) and use that input in your program. It works by reading the input and storing it in a specified variable.

Syntax:

 cin >> variable;


>> is the extraction operator, and the variable is the variable's name where the input will be stored.

Example:

  • C++

C++

#include <iostream>
#include <string>
int main() {
 std::string name;
 std::cout << "Enter your name: ";
 std::getline(std::cin, name);
 std::cout << "Hey, " << name << "!" << std::endl;
 return 0;
}


Output:

Enter your name: Ninja
Hey, Ninja!


In this example, the program prompts the user to enter their name using std::cout and then reads in their input using the std::getline function with std::cin.

2. cout (Standard output stream in C++)

 ''cout" is a tool in C++ that allows you to display output from your program. It sends the result to the standard output stream, where the user can see it. 

Syntax: 

cout<<variable; 


The << operator inserts the message into the output stream.   

Example:

  • C++

C++

#include <iostream>
int main() {
 int n, factorial = 1;
 std::cout << "Enter a positive integer: ";
 std::cin >> n;
 for (int i = 1; i <= n; i++) {
   factorial *= i;
 }
 std::cout << "Factorial of " << n << " is " << factorial << std::endl;
 return 0;
}


Output:

Enter a positive integer: 6
Factorial of 6 is 720

 

The program prints the result using std::cout.

3. cerr (Standard error stream in C++)

The C++ utility "cerr" is used to show output, but it focuses on error messages. The standard output stream receives output from "cerr". Yet, it is frequently used to display error messages or other output kinds that need to go to the standard error stream rather than the standard output stream.

Syntax: 

cerr<<variable


Example:

  • C++

C++

#include <iostream>
int main() {
 std::cerr << "There's an error" << std::endl;
 return 1;
}


Output:

There's an error

 

Std::cerr is used for error messages instead of std::cout, which is used for standard output. A return statement that has a value other than zero denotes an error.

Note: Although "cout" is used for standard program output that consumers view or programmers use to keep track of program data, "cerr" is used for error messages that programmers use to remedy issues.

4. clog (Standard logging stream in C++)

The C++ tool "clog" is used for logging messages and showing output. Clog delivers output to the standard output stream similarly to cout and cerr; however, it is often employed for logging messages that are not necessarily errors but are nonetheless significant to record.

Syntax: 

clog<<variable;


Example:

  • C++

C++

#include <iostream>
int main() {
 std::clog << "In Debug" << std::endl;
 return 0;
}


Output

In Debug

 

That std::clog is typically used for logging or debugging messages; it is not buffered, meaning the messages are immediately displayed on the console.

Note:Whereas "cout" is used to show information to the user or log program data, "clog" is used to write programmers' error messages or diagnostic data. "cout" is used for typical program output that users view or to track program data, whereas "clog" aids in program debugging and troubleshooting.

Global Stream Objects of C++

C++ contains many pre-defined classes that deal with handling files and input-output. Stream classes have stream objects, which programmers can access with the help of iostream. The objects which are available worldwide are known as Global Stream Objects.

#include iostream declares the following two sets of eight global stream objects: Narrow Characters and Wide Characters.

Narrow Characters

  • They focus on Bytes. 
  • The objects can hold 256 values internally and are mapped to an entry in the ASCII table. 
  • They occupy less memory (8 bits only).
  • The objects in this group are cin, cout, cer, and clog.

Wide Characters

  • They have a wide focus. 
  • Internally, the objects employ the wchar t type, equivalent to UNICODE values and far greater value range than the little character type. 
  • They take up more memory than the little characters.
  • The objects in these groups are wcin, wcout, wcerr, and wclog.

wcin

wcin is an input stream object of the “wistream” class in C++. This allows us to read wide characters. Let’s take an example of wcin.

  • C++

C++

#include <iostream>
using namespace std;

int main() {
int age;
wcin >> age;

return 0;
}


Explanation

wcin stores the input from the user “age” as a variable.

wcout

wcout is an output stream object of the “wostream” class in C++. It allows us to write wide characters to the standard output stream. Let’s take an example of wcout.

  • C++

C++

#include <iostream>
using namespace std;

int main() {
int num = 10;
wcout << num;
return 0;
}


Output

10


Explanation

wcout outputs the value of the variable num to the standard output stream using ‘wcout’ object.

wcerr

wcerr is an output stream object of the “ostream” class in C++ that is used to write wide characters to the error messages to the console. Below is an example of wcerr.

  • C++

C++

#include <iostream>
using namespace std;

int main() {
wcerr << L"There is an error with the input";
return 0;
}


Output

There is an error with the input


Explanation

Here, we write the error message as a wide character string.

wclog

wclog is an output stream object of the “ostream” class in C++ that is used to write wide characters for logging or displaying informational messages to the console. Let’s take a look at the example of wclog.

  • C++

C++

#include <iostream>
using namespace std;

int main() {
int item = 5;
wclog << L"Processing " << item <<L" items";
return 0;
}


Output

Processing 5 items


Explanation

Here, a message is logged indicating the items which are still in process in wide characters.

More Examples of <iostream>

1. Using Classic <iostream>

Below is the example code for using classic <iostream>.

  • C++

C++

#include <iostream.h>
int main(){
cout << "This is an example of classic iostream";
return 0;
}


Output

This is an example of classic iostream


Explanation

Classic iostream uses “.h” at the end of the header. They put their declarations in the global namespace. 

Note: Classic iostream were used in older version of C++ compilers and are not used nowadays.

2. Using Standard <iostream>

Now we will discuss an example of using standard <iostream>.

  • C++

C++

#include <iostream>
int main(){
std::cout << "This is an example of standard iostream";
return 0;
}


Output

This is an example of standard iostream


Explanation

Standard iostream allows input and output operations in C++. Here, we use “std::cout” to display text. Here we does not include “.h” which was the case in classic iostream.

​​3. Forward Declaration with Standard <iostream>

Below is the example code for the forward declaration with standard <iostream>. 

  • C++

C++

#include <iosfwd>
using std::istream;
class MyClass;
istream& operator>>(istream&, MyClass&);


Explanation

We include a forward declaration of “istream” using <iosfwd> header and declares a function prototype for overloading the input stream extraction operator.

4. Forward Declaration with Classic <iostream>

Below is the example code for the forward declaration with classic <iostream>.

  • C++

C++

#include <iostream.h>
class MyClass;
istream& operator>>(istream&, MyClass&);


Explanation

Here, we use the classic approach with “<iostream.h>” to forward declare the class “MyClass” and overload the input stream extraction operator.

5. Below is the example code for Both Classic and Standard <iostreams>

  • C++

C++

#include <iostream.h>
class Library;
istream& operator>>(istream&, Library&);
ostream& operator<<(ostream&, const Library&);


Explanation

Here, we declare the prototype for the insertion and extraction stream operators for the class “Library”. It allows input and output using >> and << operators.

Why Should We Use <iostream> Instead of the Traditional <cstdio>?

<iostream> is better than <cstdio> in many ways, as it provides the user more type safety, helps in reducing error, it also allows extensibility, and provides inheritability.

Let's discuss this one by one.

1. Type-Safety

<iostream> provides more type safety than <cstdio> as the type of object is known by the compiler instead of using '%' for the identification.

2. Error Prone

<iostream> is less error-prone as it removes the redundancy in the code since it reduces the errors.

3. Extensible

You can use the new user-defined type via <Iostream> without breaking the existing code.

4. Inheritable

<cstdio>’s file are the real classes while C++ <iostream> are made from the real classes such as std :: ostream and std:: istream. 

Frequently Asked Questions

Why do we use #include iostream in C++?

We use #include <iostream>" in C++ code to integrate the input-output stream library. It allows input and output operations functionalities, such as reading data from or writing data to the screen (console) or files.

Is #include iostream a function?

No, #include <iostream> is not a function. It's a preprocessor directive. This directive guides the compiler to contain the iostream header, essential for handling input and output tasks.

What is the use of #include iostream in C?

#include <iostream> is a keyword used to include the iostream header file. The iostream header file contains all the functions used for reading and writing operations like cin and cout.

What is header file in C++?

The C++ header file contains all the declarations for the class. It contains all the information about the class, its members and all the details. Also, C++ distinguishes between the declaration and definition. So in the header file in C++, you have declarations for the class.  

Conclusion 

Throughout this article, we have provided a detailed overview of the include iostream in C++. We have covered its main features and uses, including input/output operations, manipulators, and formatting. Our goal has been to help you, ninjas, understand this necessary header file better, include iostream in C++, and use it effectively in your programs.

If you would like to explore this topic in more detail, we would recommend that you refer to the following articles:

Moreover, apart from this blog, you can read more such articles on our platform, Coding Ninjas Studio. If you want to improve or practice coding questions visit our website Coding Ninjas and grab your dream jobs in product-based companies. For interview preparation, you can read Interview Experiences of popular companies. 

Happy Coding!

Previous article
Difference Between C++ and Java
Next article
Decision Making in C / C++ (if , if..else, Nested if, if-else-if )
Live masterclass