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

C++ में cerr और clog

cerr और clog error और log messages के लिए special channels हैं, normal output से अलग, main announcement speaker के बगल में एक warning speaker की तरह। यह problems को results के साथ मिलने से रोकता है।
Syntax
cpp
std::cerr << "error message" << std::endl;

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

Standard Error Stream (cerr)

cerr standard error stream है, specifically normal program output की बजाय problems report करने के लिए बनाया गया।

खासकर, cerr unbuffered है, इसलिए इसमें लिखा हर message flush होने का इंतज़ार करने की बजाय तुरंत screen पर दिखता है — exactly वही जो आप एक crash या एक fatal error report करते समय चाहते हैं।

उदाहरण: 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 standard log stream है, diagnostic या status messages के लिए intended जो cerr के instant display की ज़रूरत जितने urgent नहीं हैं।

cerr के उलट, clog buffered है, मतलब यह output को memory में collect करता है और efficient batches में लिखता है, जो frequent logging के लिए बेहतर suited है जिसे आपके program को धीमा नहीं करना चाहिए।

उदाहरण: 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 बनाम cout

cout और cerr दोनों terminal पर print करते हैं, लेकिन उन्हें अलग रखना आपको एक को दूसरे के बिना redirect करने देता है — उदाहरण के लिए, cout को एक results file में भेजना जबकि cerr फिर भी screen पर errors दिखाता रहे।

यही separation exactly वह वजह है कि command-line tools पहली जगह output को errors से अलग करते हैं।

उदाहरण: 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 बनाम cerr

clog और cerr दोनों conceptually same error destination target करते हैं, लेकिन urgency और performance में अलग हैं: cerr तब इस्तेमाल करें जब एक message को होते ही तुरंत दिखना ज़रूरी हो, जैसे एक fatal error, और clog background diagnostic logging के लिए जहाँ buffering से एक छोटी delay बिल्कुल acceptable है।

उदाहरण: 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;
}

Runtime Errors Handle करना

cout की बजाय cerr में errors लिखना guarantee करता है कि वे अभी भी visible रहें भले ही एक user आपके program का normal output myprogram > results.txt जैसी किसी चीज़ से एक file में redirect कर दे, क्योंकि वह redirection सिर्फ cout को affect करता है, cerr को नहीं।

यही वजह है कि well-behaved command-line programs कभी error messages को अपने cout output में मिलाते नहीं।

उदाहरण: 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. #}
आम गलतियां
  1. error messages के लिए cout इस्तेमाल करना, जो फिर normal output और redirections में मिल जाते हैं, जब errors cerr पर होने चाहिए।
  2. यह उम्मीद करना कि cerr output cout की तरह buffered होगा, जब यह unbuffered है और तुरंत दिखता है।
  3. यह मान लेना कि cerr program रोक देता है, जब यह सिर्फ print करता है और program चलता रहता है।
🔒

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.