← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 7 of 15

C++ के Comments

Comments आपके code में लोगों के पढ़ने के लिए notes हैं, और computer इन्हें ignore करता है। यह अपने homework के margin में एक reminder लिखने जैसा है।
Syntax
cpp
// single-line comment

/* multi-line
   comment */

Single-Line Comments

Single-line comments // से शुरू होते हैं और उस line के अंत तक फैलते हैं, इन्हें उस code के बगल में quick, one-off notes के लिए ideal बनाते हुए जिन्हें वे describe करते हैं।

Compiler compile करने से पहले इन्हें पूरी तरह strip कर देता है, इसलिए इनका आपके program की performance या behavior पर zero असर होता है।

उदाहरण: Single-Line Comments

cpp
#include <iostream>

int main() {
	// Print the greeting
	std::cout << "Hello" << std::endl;
	return 0;
}

Multi-Line Comments

Multi-line comments /* से खुलते हैं और */ से बंद होते हैं, आपको कई lines में फैले code के एक पूरे block को comment out या explain करने देते हुए।

// के उलट, ये एक line break पर automatically खत्म नहीं होते, इसलिए एक missing closing */ अगले तक की हर चीज़ को accidentally comment out कर देगा।

उदाहरण: Multi-Line Comments

cpp
#include <iostream>

/*
This block explains the program below.
It can span as many lines as needed.
*/
int main() {
	std::cout << "Hello" << std::endl;
	return 0;
}

Code Document करना

अच्छे comments यह explain करते हैं कि code का कोई हिस्सा *क्यों* exist करता है या एक particular approach क्यों चुना गया, सिर्फ line-by-line code *क्या* करता है यह नहीं — code खुद पहले ही यह दिखा देता है।

इस तरह का context cookiescursor.com जैसे shared codebase पर खासकर valuable है, जहाँ किसी दूसरे developer को महीनों बाद आपकी reasoning समझने की ज़रूरत पड़ सकती है।

उदाहरण: Documenting Code

cpp
#include <iostream>

int main() {
	// Using a cache here because this codebase queries this value
	// thousands of times per request -- not just for style.
	int cachedValue = 42;
	std::cout << cachedValue << std::endl;
	return 0;
}

Code को Comment Out करना

code के एक block को /* */ में wrap करना debugging के दौरान इसे बिल्कुल delete किए बिना temporarily disable करने का एक quick तरीका है, आपको यह test करने देते हुए कि क्या उस logic को हटाना एक problem fix करता है।

बस बाद में इसे un-comment करना याद रखें, या अगर change permanently रहनी हो तो इसकी बजाय version control इस्तेमाल करें।

उदाहरण: Commenting Out Code

cpp
#include <iostream>

int main() {
	std::cout << "Active code" << std::endl;
	/*
	std::cout << "Temporarily disabled while debugging" << std::endl;
	*/
	return 0;
}

Comment Best Practices

Stale हो चुके comments — जो behavior बताते हैं जो code के पास अब नहीं है — अक्सर कोई comment न होने से भी बुरे होते हैं, क्योंकि वे actively अगले reader को mislead करते हैं।

Comments को छोटा रखें, जब भी वे describe करने वाली logic बदले उन्हें update करें, और i++ के ऊपर // increment i जैसे obvious code को सिर्फ दोबारा बताने वाले comments से बचें।

उदाहरण: Comment Best Practices

cpp
#include <iostream>

int main() {
	int i = 0;
	i++; // advance to the next retry attempt (not just "increment i")
	std::cout << i << std::endl;
	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. /* */ comments को nest करने की कोशिश करना, जहाँ पहला */ पूरे comment को खत्म कर देता है और बाकी compile न होने वाला code बन जाता है।
  2. एक // comment line को एक backslash से खत्म करना, जो comment को अगली line तक जारी रखता है और चुपचाप real code को comment out कर देता है।
  3. पुराना commented-out code आस-पास छोड़ देना जो अब program से match नहीं करता, जो readers को मदद करने से ज़्यादा confuse करता है।

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.