← Back to C Course | Chapter 12: Advanced Topics | Lesson 17 of 20

C की Debugging Techniques

Debugging आपके program में mistakes ढूंढना और fix करना है, एक detective होने जैसा। Values print करना और checks add करना आपको दिखाता है क्या हो रहा है।
Syntax
c
printf("DEBUG: variable = %d\n", variable);
printf("at %s:%d\n", __FILE__, __LINE__);

#ifdef DEBUG
    printf("verbose debug output\n");
#endif

Printf Debugging

अपने code में key points पर variables की current values print करने के लिए printf() calls insert करना C में सबसे simple और सबसे universal debugging technique है, किसी special tool की ज़रूरत नहीं, हालांकि इसका मतलब है final program ship करने से पहले extra output हटाना या comment out करना याद रखना।

उदाहरण: Printf Debugging

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Print formatted text to the screen
	printf("DEBUG: x = %d\n", x);
	x = x * 2;
	// Print formatted text to the screen
	printf("%d", x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Predefined Macros इस्तेमाल करना

Predefined macros __FILE__ और __LINE__ automatically current source file के नाम और current line number में expand होते हैं, इसलिए इन्हें एक debug print में embed करना (जैसे printf("%s:%d value=%d\n", __FILE__, __LINE__, value);) आपको exactly बताता है कि एक given message कहाँ से आया बिना इसे manually type किए।

उदाहरण: Using Predefined Macros

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare value and set it to 5
	int value = 5;
	// Print formatted text to the screen
	printf("%s:%d value=%d\n", __FILE__, __LINE__, value);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Conditional Debug Blocks

debug output को #ifdef DEBUG ... #endif blocks में wrap करना आपको same source file को development builds के लिए verbose diagnostic printing enabled के साथ और release builds के लिए पूरी तरह strip out करके compile करने देता है, सिर्फ compile time पर DEBUG macro define या omit करके।

उदाहरण: Conditional Debug Blocks

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro DEBUG (replaced by the preprocessor)
#define DEBUG
// Program execution starts in main()
int main() {
	#ifdef DEBUG
	// Print formatted text to the screen
	printf("Debug output enabled\n");
	#endif
	// Print formatted text to the screen
	printf("Normal output");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pointer Safety Checks

एक uninitialized pointer कोई भी garbage address रखता है जो memory में पहले से था, इसलिए इसे dereference करना unpredictably crash कर सकता है या, बदतर, चुपचाप unrelated memory corrupt कर सकता है।

हर pointer को तुरंत NULL initialize करना और dereference करने से पहले NULL check करना एक random crash को एक predictable, debuggable में बदल देता है।

उदाहरण: Pointer Safety Checks

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int *ptr = NULL;
	// Check whether ptr == NULL
	if (ptr == NULL) {
		// Print formatted text to the screen
		printf("Pointer not initialized safely -- avoided dereference");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Dry Run Loop Traces

Off-by-one और boundary errors C में सबसे common loop bugs में से हैं, इसलिए debug करते समय हर iteration पर loop index print करना जल्दी दिखा देता है कि एक loop एक बार ज़्यादा चल रहा है, एक बार कम, या array की valid range से बाहर एक index access कर रहा है।

उदाहरण: Dry Run Loop Traces

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Print formatted text to the screen
		printf("Loop index: %d\n", i);
	}
	// 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. finished program में debug printf calls छोड़ देना।
  2. compiler warnings ignore करना, जो अक्सर real bugs की ओर point करते हैं।
  3. पहले यह check किए बिना pointer या array values print करना कि वे valid हैं।

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.