Back to results for “I/O Streams”
C++ › I/O Streams

What is the difference between std::cout, std::cerr, and std::clog?

Beginner

Answer

std::cout is the standard output stream (buffered). std::cerr is the standard error stream, unbuffered so messages appear immediately (good for errors). std::clog is also for logging to standard error but buffered (more efficient for high-volume logs). Separating normal output from errors lets them be redirected independently.
std::cout << "result\n";
std::cerr << "error!\n";   // immediate, unbuffered
Real-world example Errors go to std::cerr so they still appear even if stdout is redirected to a file.

Common follow-ups: Which is unbuffered? | Why separate output from errors?