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++
#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;
}
You can also try this code with Online C++ Compiler
Run Code
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++
#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;
}
You can also try this code with Online C++ Compiler
Run Code
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++
#include <iostream>
int main() {
std::cerr << "There's an error" << std::endl;
return 1;
}
You can also try this code with Online C++ Compiler
Run Code
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++
#include <iostream>
int main() {
std::clog << "In Debug" << std::endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
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++
#include <iostream>
using namespace std;
int main() {
int age;
wcin >> age;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
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++
#include <iostream>
using namespace std;
int main() {
int num = 10;
wcout << num;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
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++
#include <iostream>
using namespace std;
int main() {
wcerr << L"There is an error with the input";
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
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++
#include <iostream>
using namespace std;
int main() {
int item = 5;
wclog << L"Processing " << item <<L" items";
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
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++
#include <iostream.h>
int main(){
cout << "This is an example of classic iostream";
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
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++
#include <iostream>
int main(){
std::cout << "This is an example of standard iostream";
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
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++
#include <iosfwd>
using std::istream;
class MyClass;
istream& operator>>(istream&, MyClass&);
You can also try this code with Online C++ Compiler
Run Code
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++
#include <iostream.h>
class MyClass;
istream& operator>>(istream&, MyClass&);
You can also try this code with Online C++ Compiler
Run Code
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++
#include <iostream.h>
class Library;
istream& operator>>(istream&, Library&);
ostream& operator<<(ostream&, const Library&);
You can also try this code with Online C++ Compiler
Run Code
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!