cerr and clog: Error Streams in C++
Beyond cout, C++ provides std::cerr (errors, unbuffered) and std::clog (logs, buffered) — all three usually reach the same terminal, but they are separate streams the caller can redirect independently. The classic split: program output goes to cout so it can be piped into files or other programs, while diagnostics go to cerr so they still reach the human.
cerr is unbuffered (every write appears immediately, even mid-crash), while clog buffers like cout for efficiency. Reaching for cerr for errors is what makes ./app > out.txt capture results while errors still print to the screen.
Three streams, three jobs
cout carries normal output, cerr carries errors immediately, clog carries buffered logging — same syntax (<<), different destinations.
Redirection splits them
./app > results.txt sends only cout to the file; cerr keeps printing to the terminal, so errors are never buried in data.
cerr is unbuffered
Every cerr write hits the terminal at once — slower, but crash-safe, which is exactly what error reporting needs.
clog for volume
Routine logging belongs on clog: buffered like cout for speed, yet separable from real output when redirected.
#include <iostream> int main() { int value = -3; if (value < 0) { std::cerr << "error: negative value" << std::endl; return 1; } std::cout << value << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.