← Back to C++ Course | Chapter 2: Input & Output | Lesson 4 of 8

C++ cerr & clog

cerr and clog are special channels for error and log messages, separate from normal output, like a warning speaker beside the main announcement speaker. It keeps problems from mixing with results.
Syntax
cpp
std::cerr << "error message" << std::endl;

std::clog << "log message" << std::endl;

Standard Error stream (cerr)

cerr is the standard error stream, meant specifically for reporting problems rather than normal program output.

Crucially, cerr is unbuffered, so every message written to it appears on screen instantly rather than waiting to be flushed — exactly what you want when reporting a crash or a fatal error.

Example: Standard Error stream (cerr)

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	std::cerr << "Fatal error: file not found" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Log Stream (clog)

clog is the standard log stream, intended for diagnostic or status messages that aren't urgent enough to need cerr's instant display.

Unlike cerr, clog is buffered, meaning it collects output in memory and writes it in efficient batches, which is better suited to frequent logging that shouldn't slow down your program.

Example: Log Stream (clog)

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	std::clog << "Status: request processed" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

cerr vs cout

cout and cerr both print to the terminal, but keeping them separate lets you redirect one without the other — for example, sending cout to a results file while cerr still shows errors on screen.

This separation is exactly why command-line tools distinguish output from errors in the first place.

Example: cerr vs cout

cpp
#include <iostream>

int main() {
	std::cout << "Result: 42" << std::endl; // can be redirected separately
	std::cerr << "Warning: low disk space" << std::endl;
	return 0;
}

clog vs cerr

clog and cerr both target the same error destination conceptually, but differ in urgency and performance: reach for cerr when a message needs to appear the instant it happens, like a fatal error, and clog for background diagnostic logging where a small delay from buffering is perfectly acceptable.

Example: clog vs cerr

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	std::cerr << "Immediate: connection lost" << std::endl;
	std::clog << "Background: cache refreshed" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Handling Runtime Errors

Writing errors to cerr instead of cout guarantees they're still visible even if a user redirects your program's normal output to a file with something like myprogram > results.txt, since that redirection only affects cout, not cerr.

This is why well-behaved command-line programs never mix error messages into their cout output.

Example: Handling Runtime Errors

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Normal output" << std::endl;
	std::cerr << "Error output stays visible even if cout is redirected" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using cout for error messages, which then get mixed into normal output and redirections, when errors belong on cerr.
  2. Expecting cerr output to be buffered like cout, when it is unbuffered and appears immediately.
  3. Assuming cerr stops the program, when it only prints and the program keeps running.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.