1. Intro
cerr and clog are standard stream objects, like cout, used to write output to the standard error stream instead of standard output. They are commonly used for reporting errors, warnings, and diagnostic/log messages, keeping them separate from normal program output.
Cricket analogy: A stadium PA announcer keeps urgent safety alerts on a separate channel from the regular match commentary, just as cerr and clog route error and diagnostic messages to a separate stream from cout's normal program output.
2. Syntax
cerr << "error message";
clog << "log message";3. Explanation
Both cerr and clog write to the standard error stream (stderr), but they differ in buffering. cerr is unbuffered by default, meaning each message is flushed and displayed immediately — ideal for critical errors that must be seen right away, even if the program crashes afterward. clog is buffered, so its output may be held temporarily and flushed later (e.g., when the buffer is full or the program ends normally), which is more efficient for routine logging where instant display is not critical.
Cricket analogy: cerr is like an umpire's immediate signal for a serious infraction — shown the instant it happens, unbuffered, even if the match is abandoned right after — while clog is like a scorer's routine notes jotted down and only compiled into the official record at the end of the innings, buffered for efficiency.
Because cerr is unbuffered, using it heavily for non-critical messages can slow down a program. Prefer clog for high-volume logging and reserve cerr for genuine, immediate error reporting.
4. Example
#include <iostream>
using namespace std;
int main() {
int divisor = 0;
clog << "Starting division check..." << endl;
if (divisor == 0) {
cerr << "Error: division by zero attempted!" << endl;
return 1;
}
cout << "Result: " << (10 / divisor) << endl;
return 0;
}5. Output
Starting division check...
Error: division by zero attempted!6. Key Takeaways
cerrandclogboth write to the standard error stream, separate fromcout.cerris unbuffered, so messages appear immediately — best for urgent errors.clogis buffered, so messages may be delayed — best for routine logging.- Redirecting
stdoutin a shell does not redirectcerr/clogoutput, since they targetstderr.
Practice what you learned
1. Which stream is unbuffered by default?
2. What is `clog` best suited for?
3. Both `cerr` and `clog` write to which stream?
4. Why might `cerr` slow a program if overused?
5. Which header declares `cerr` and `clog`?
Was this page helpful?
You May Also Like
cin and cout in C++
Learn how `cin` and `cout` handle standard input and output in C++ using the stream extraction and insertion operators, with chaining and gotchas.
I/O Manipulators in C++
Explore C++ I/O manipulators from `<iomanip>` like `setw`, `setprecision`, `setfill`, and `fixed` used to control stream formatting.
Unformatted I/O Functions in C++
Learn C++ unformatted input/output functions like `cin.get()`, `cin.put()`, and `cin.ignore()` for low-level, character-based stream handling.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics