C की Debugging Techniques
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- finished program में debug
printfcalls छोड़ देना। - compiler warnings ignore करना, जो अक्सर real bugs की ओर point करते हैं।
- पहले यह check किए बिना pointer या array values print करना कि वे valid हैं।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions