cerr and clog in C++
Written by
The Standard Error Stream (cerr) in C++:
The Standard Error Stream or cerr is a predefined object of the ostream class. C++ programs are written by humans and the humans are prone to commit errors, so the standard error stream or cerr is attached to the standard error device, which is also a display screen, just like the standard output stream cout.
However, the cerr is un-buffered and each stream insertion to cerr causes its output to appear immediately.
The cerr is also used in conjunction with the stream insertion operator << as shown in the following example.
Example:
#include <iostream.h>
int main() {
char str[] = "Unable to read.";
cerr << "Error message : " << str << endl;
}
When the above code is compiled and executed, it produces the following result −
Error message : Unable to read.
The Standard Log Stream (clog) in C++:
The predefined object of the Standard Log Stream, clog is also an instance of ostream class. In order to use it in a C++ program, one must include the iostream header file.
The clog object is said to be attached to the standard error device, which is also a display screen but the object clog is buffered.
This means that each insertion to clog could cause its output to be held in a buffer until the buffer is filled or until the buffer is flushed. It works in a similar manner to the standard error stream object, cerr.
The clog is also used in conjunction with the stream insertion operator as shown in the following example.
Example:
#include <iostream.h>
int main()
{
char str[] = "Unable to read.";
clog << "Error message : " << str << endl;
}
When the above code is compiled and executed, it produces the following result −
Error message: Unable to read.