Difference between cerr and clog
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.
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.
|1.||It is an unbuffered standard error stream||It 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).|
Please Login to comment...