C++ के Comments
In this page:
// 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
#include <iostream>
int main() {
// Print the greeting
std::cout << "Hello" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Multi-Line Comments
Multi-line comments /* से खुलते हैं और */ से बंद होते हैं, आपको कई lines में फैले code के एक पूरे block को comment out या explain करने देते हुए।
// के उलट, ये एक line break पर automatically खत्म नहीं होते, इसलिए एक missing closing */ अगले तक की हर चीज़ को accidentally comment out कर देगा।
उदाहरण: Multi-Line Comments
#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;
}
Login to try C/C++/Java/PHP code in the editor
Code Document करना
अच्छे comments यह explain करते हैं कि code का कोई हिस्सा *क्यों* exist करता है या एक particular approach क्यों चुना गया, सिर्फ line-by-line code *क्या* करता है यह नहीं — code खुद पहले ही यह दिखा देता है।
इस तरह का context cookiescursor.com जैसे shared codebase पर खासकर valuable है, जहाँ किसी दूसरे developer को महीनों बाद आपकी reasoning समझने की ज़रूरत पड़ सकती है।
उदाहरण: Documenting Code
#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;
}
Login to try C/C++/Java/PHP code in the editor
Code को Comment Out करना
code के एक block को /* */ में wrap करना debugging के दौरान इसे बिल्कुल delete किए बिना temporarily disable करने का एक quick तरीका है, आपको यह test करने देते हुए कि क्या उस logic को हटाना एक problem fix करता है।
बस बाद में इसे un-comment करना याद रखें, या अगर change permanently रहनी हो तो इसकी बजाय version control इस्तेमाल करें।
उदाहरण: Commenting Out Code
#include <iostream>
int main() {
std::cout << "Active code" << std::endl;
/*
std::cout << "Temporarily disabled while debugging" << std::endl;
*/
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Comment Best Practices
Stale हो चुके comments — जो behavior बताते हैं जो code के पास अब नहीं है — अक्सर कोई comment न होने से भी बुरे होते हैं, क्योंकि वे actively अगले reader को mislead करते हैं।
Comments को छोटा रखें, जब भी वे describe करने वाली logic बदले उन्हें update करें, और i++ के ऊपर // increment i जैसे obvious code को सिर्फ दोबारा बताने वाले comments से बचें।
उदाहरण: Comment Best Practices
#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;
}
Login to try C/C++/Java/PHP code in the editor
/* */comments को nest करने की कोशिश करना, जहाँ पहला*/पूरे comment को खत्म कर देता है और बाकी compile न होने वाला code बन जाता है।- एक
//comment line को एक backslash से खत्म करना, जो comment को अगली line तक जारी रखता है और चुपचाप real code को comment out कर देता है। - पुराना commented-out code आस-पास छोड़ देना जो अब program से match नहीं करता, जो readers को मदद करने से ज़्यादा confuse करता है।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: