Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Difference between cerr and clog

  • Last Updated : 18 Oct, 2021

In C++ input and output are performed in the form of a sequence of bytes or more commonly known as streams. cerr and clog both are associated with the standard C error output stream stderr but the cerr is the unbuffered standard error stream whereas the clog is the buffered standard error stream. In this article, we will learn what is the difference between these two streams in detail with examples.

cerr: It is the unbuffered standard error stream that is used to output the errors.  This is an object of the ostream class similar to cout.  It is unbuffered i.e, it is used when there is a need to display the error message immediately. As there is no buffer so it cannot store the error message to display it later.  So simply as cerr is unbuffered it can not store the message.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.

Example:

C++






// c++ program to implement
// the above approach
#include <iostream>
using namespace std;
  
// Driver code
int main()
{
    cerr << "the message displayed is unbuffered";
    return 0;
}

Output:

Output

clog: It is the buffered standard error stream that is used to output the errors.  This is also an object of the ostream class similar to cout.  It is buffered i.e, firstly the error message is inserted in the buffer, and then it is displayed on the screen. As there is a buffer so it can store the error message to display it later unlike cerr.  So simply as clog is buffered it can not display the message immediately. clog is commonly used for logging purposes. For non-critical event logging, efficiency is more important so clog is preferred to cerr.

Example:

C++




// C++ program to implement
// the above approach
#include <iostream>
using namespace std;
  
// Driver code
int main()
{
    clog << "the message displayed is buffered";
    return 0;
}

Output:

Output

Difference Table:

 

cerr

clog

1.It is an unbuffered standard error streamIt is a buffered standard error stream
2.It is used for displaying the error.It is used for logging.
3.It is used to display the message immediately. It can not display the message immediately.
4.It can not store the message to display it later.It can store the message in the buffer to display it later.
5.The “c” in cerr refers to “character” and ‘err’ means “error”, Hence cerr means “character error”. The “c” in clog refers to “character” and ‘log’ refers to “logging”, hence clog means “character logging”.
6.It is less efficient than clog because it is unbuffered output.It is more efficient than cerr because it is buffered output.
7.It is preferred for critical errors (errors that can cause system crashes).It is not preferred for critical errors (errors that can cause system crashes).



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!