Cerr in C++28 Aug 2024 | 3 min read Various streams are available in the C++ standard library for handling input and output activities. One of these streams is called cerr, which is short for "standard error". Cerr is made specifically for error messages and diagnostics, unlike the cout stream, which is used for general output. In this article, we'll investigate cerr's features, go over its syntax and C++ usage and give code samples with their related results. The standard error device is connected to the cerr stream, which is commonly the console or terminal. During the execution of a C++ program, it is mostly used to output error messages, warnings, and other diagnostic data. Each character or message is displayed on the output device without delay by default because cerr is not buffered. You must include the <iostream> header file in your C++ program to use cerr, which gives access to C++'s standard input/output capability. Here is an illustration of how to use the required headers: The operator can be used to write messages to the cerr stream in a manner similar to how it would be used with the cout stream. The following is the syntax for writing to cerr: Syntax:Here, message is the error message or diagnostic data you want to display, and std::cerr stands for the standard error stream. Let's look at an illustration: Example:Output: An error occurred! Explanation: In the above example, the operator is used to write the error message "An error occurred!" to the cerr stream. The console immediately shows the message. Similar tocout, cerr allows you to prepare output using a variety of manipulators given by the iomanip header. For instance, you can alter the output's width and alignment, as well as the precision of floating-point figures. Here is an illustration of formatting in cerr: Example:Output: The value of pi is: 3.1416 Using std::setprecision(4) and std::fixed, the output precision was set to 4 decimal places in the code. It ensures the desired level of precision when displaying the value of pi. Using rdbuf() functionThe console is connected by default to the cerr stream, which is typically the standard error device. The cerr command can be redirected to a file to record error messages and diagnostics. You can accomplish this by utilizing the rdbuf() function and the file stream (<fstream>). Here's an illustration: Example: Output: The error The following message will appear in the log file "error.log": An error occurred! Explanation: We construct an ofstream object called errorLog and link it to the file "error.log" in the code. After that, the cerr stream is forwarded to the errorLog stream buffer using the rdbuf() method. Therefore, any messages sent to cerr will be sent to the designated log file rather than the terminal. The processing and reporting of errors is one of cerr's most prevalent use cases. You can use cerr to give the user helpful information about an error that happens while a program is running. You can make sure that error messages are visible and distinct from regular program output by exporting them to cerr. Here's an illustration: Example:Output: Error: Division by zero! Explanation: In this demonstration, we try to divide 10 by the variable "divisor" starting value, which is set to zero. Since division by zero is prohibited, if the divisor is 0, we send an error message to cerr. ConclusionIn this blog post, we looked at the C++ cerr stream, which is used as the standard error stream for diagnostics and error messages. We discussed its syntax and usage with code samples showcasing cerr's capabilities. Additionally, we went over formatting the output and rerouting cerr to a file. Using cerr correctly may improve error handling and give users useful information while running your C++ programs. Next TopicMake_shared in C++ |
Different ways to print elements of vector in C++
In this article, you will learn about the different ways to print elements of vectors in C++. But before discussing the different ways, you must know about the Vectors with their advantages and disadvantages. What are Vectors? Vectors are similar to dynamic arrays in that the container manages...
5 min read
Minimum Spanning Tree using Kruskal's Algorithm in C++
Introduction of the Kruskal's Algorithm: In the fast-changing world of tech and info, algorithms are super important for solving hard problems. One cool algorithm that's simple and works well is Kruskal's algorithm. It comes from graph theory and is great for finding the smallest way to connect...
11 min read
std::thread detach in C++
In today's software development landscape, multithreading is a fundamental concept that allows you to harness the full potential of modern, multi-core processors. For C++ developers, the Standard Library provides robust tools for managing threads, with std::thread being a central figure. One crucial aspect of working with...
6 min read
Banker's Algorithm in C++
The Banker's method is a resource allocation and deadlock avoidance method that is used in operating systems to guarantee that operations are carried out effectively and securely in a multi-resource environment. Edsger W. Dijkstra created it in 1965, and it is essential for managing resources including...
15 min read
ratio_greater() function in C++
In this article, you will learn about the with its syntax, parameters, and examples. The ratio_greater() is a built-in C++ function determining whether ratio R1 is more significant than ratio R2. The Boolean constant "value" is returned; if ratio 1 is more significant than ratio 2,...
4 min read
Vector Pair in C++
What is a Vector in C++? In C++, a vector is a sequence container that stores elements of the same type in a contiguous block of memory. Each element in a vector is assigned a numerical index, which is used to access the element. Vectors are similar...
4 min read
std::future in C++
One of the most useful tools are available in the C++ Standard Library for multithreading and asynchronous programming is std:: future. This part is essential to handling asynchronous operations and getting output from jobs that are running concurrently. Included in the C++11 concurrency utilities, it provides...
4 min read
Anonymous objects in C++
Introduction to Anonymous objects, or unidentified or temporary objects, are fundamental concepts in C++ programming. They refer to instances of a class that are created without assigning them to a named variable. Instead, they are used directly in expressions or function calls, serving a temporary purpose. The...
8 min read
Queue using stack in C++
In this article, you will learn about the queue using stack in C++ with its implementation. Implementing a stack data structure as a queue, with the low-level data structure being the push (adding an element) and pop (removing an element) operations. A stack is a Last In First...
3 min read
Difference between break and continue in C++
A loop control statement used to end a loop in C++ is called a break. The loop iterations end as soon as the break statement is met from inside the loop, and control is instantly transferred from the loop to the first statement after the loop. break;...
7 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India