#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++.
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.
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.
In this example, the "cin" function reads an integer value entered by the user and stores it in the "age" variable.
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.
In this example, the "cout" function displays the message "Hey, Ninjas!!" on the console or terminal.
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.
- 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.
- 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.
cin >> variable;
>> is the extraction operator, and the variable is the variable's name where the input will be stored.
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.
The << operator inserts the message into the output stream.
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.
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.
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 <iostream> in 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.
They focus on Bytes. The objects can hold 256 values internally and are mapped to an entry in the ASCII table. They occupy the memory of 8 bits only.
The objects in this group are cin, cout, cer, and clog.
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 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.
wcin stores the input from the user “age” as a variable.
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.
wcout outputs the value of the variable num to the standard output stream using ‘wcout’ object.
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.
There is an error with the input
Here, we write the error message as a wide character string.
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.
Processing 5 items
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>.
This is an example of classic iostream
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>.
This is an example of standard iostream
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>.
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>.
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>
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.
<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.
You can use the new user-defined type via <Iostream> without breaking the existing code.
<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
What is #include <iostream> in C++?
#include <iostream> is a keyword used to include the iostream header file. The iostream header file is a file that contains all the functions used for input and output operations like cin and cout.
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 in the iostream library C++?
The iostream library in C++ includes classes such as "iostream," "istream," and "ostream."These classes define the standard input and output streams for data reading and writing to the console and files. They facilitate input and output procedures in C++ programs.
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:
- Header Files
- Ternary Operator in C
- Bubble Sort Program in C
- C++ Interview Questions
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.