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

C में Comments

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

/* multi-line
   comment */

Single-Line Comments

एक single-line comment // से शुरू होता है और compiler को उस point से line के अंत तक सब कुछ ignore करने को कहता है -- आस-पास के code को बिना disturb किए किसी specific statement के बगल में एक quick note के लिए handy।

उदाहरण: Single-Line Comments

c
#include <stdio.h>
int main() {
	// This line explains the printf below
	printf("Hello");
	return 0;
}

Multi-Line Comments

एक multi-line comment /* से शुरू होता है और */ पर खत्म होता है, और बीच की हर चीज़ ignore हो जाती है चाहे यह कितनी भी lines तक फैली हो -- logic के पूरे block को explain करने या एक साथ कई lines को temporarily disable करने के लिए useful।

उदाहरण: Multi-Line Comments

c
#include <stdio.h>
int main() {
	/* This comment
	   spans multiple lines */
	printf("Hello");
	return 0;
}

Code Document करना

अच्छे comments यह explain करते हैं कि code का कोई हिस्सा *क्यों* exist करता है या जिस तरह काम करता है वैसे क्यों करता है, सिर्फ line-by-line *क्या* करता है यह नहीं -- code खुद पहले ही दिखा देता है क्या हो रहा है, लेकिन एक tricky decision के पीछे की reasoning सिर्फ आप जानते हैं।

उदाहरण: Documenting Code

c
#include <stdio.h>
int main() {
	// Using 1024 because memory is measured in kibibytes here
	int kilobyte = 1024;
	printf("%d", kilobyte);
	return 0;
}

Code को Comment Out करना

code के किसी block को delete करने की बजाय comment markers में wrap करना आपको debug करते हुए इसे temporarily disable करने देता है, ताकि आप उस code को हमेशा के लिए खोए बिना test कर सकें कि क्या कोई specific section किसी bug का source है।

उदाहरण: Commenting Out Code

c
#include <stdio.h>
int main() {
	printf("Active code");
	// printf("Disabled while debugging");
	return 0;
}

अच्छी Comment Habits

बहुत लंबे या obvious code को दोबारा बताने वाले comments clarity की बजाय noise add करते हैं, और आस-पास का code बदलने के बाद unchanged छोड़े गए comments actively misleading बन जाते हैं -- stale comments को fix करने वाले एक bug की तरह treat करें।

उदाहरण: Good Comment Habits

c
#include <stdio.h>
int main() {
	int x = 5; // avoid: "sets x to 5" -- explain reasoning instead
	printf("%d", x);
	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 को खत्म कर देता है और बाकी बचा हुआ broken code बन जाता है।
  2. strict C89 compiler mode में // comments इस्तेमाल करना, जहाँ सिर्फ /* */ allowed है।
  3. outdated commented-out code को आस-पास छोड़ देना जो अब program actually क्या करता है इससे match नहीं करता।

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.