# डिबगिंग के मूल तत्व — C Programming Fundamentals: Pointers, Memory और Files

Source: https://www.skillbyai.com/hi/c/c-debugging

> printf, compiler warnings और gdb breakpoints व stepping से bugs खोजें।

## printf डिबगिंग

सबसे सरल तकनीक: वेरिएबल मान छापने और यह पुष्टि करने के लिए कि कौन-सी लाइनें असल में चलती हैं, `printf` कॉल जगह-जगह लगाएँ। तेज़, पर हर बार एडिट और रीकंपाइल करना पड़ता है।

## डिबग सिंबल के साथ कंपाइल करें

`-g` वह डिबग जानकारी शामिल करता है जो gdb को चाहिए; `-Wall -Wextra` चलाने से पहले ही संदिग्ध कोड सामने ला देता है।

```bash
gcc -g -Wall -Wextra prog.c -o prog
gdb ./prog
```

## एक न्यूनतम gdb सत्र

gdb के अंदर: `break main` ब्रेकपॉइंट सेट करता है, `run` प्रोग्राम शुरू करता है, `next` लाइन दर लाइन आगे बढ़ता है, और `print x` किसी वेरिएबल का मौजूदा मान दिखाता है।

## अंतिम क्विज़ 1/8

अंतिम क्विज़

**Quiz:** gcc hello.c -o hello क्या करता है?

- [ ] Runs the program
- [ ] Deletes hello.c
- [x] Compiles the source into an executable
- [ ] Installs gcc

*Answer:* Compiles the source into an executable. -o output file को नाम देता है।

## अंतिम क्विज़ 2/8

अंतिम क्विज़

**Quiz:** ints के साथ C में 7 / 2 क्या है?

- [ ] 3.5
- [ ] 4
- [ ] 2
- [x] 3

*Answer:* 3. Integer division दशमलव हटा देता है।

## अंतिम क्विज़ 3/8

अंतिम क्विज़

**Quiz:** scanf("%s") की जगह fgets क्यों उपयोग करें?

- [x] It limits input to the buffer size
- [ ] It is shorter
- [ ] It parses numbers
- [ ] It ignores spaces

*Answer:* It limits input to the buffer size. scanf %s में bounds checking नहीं है।

## अंतिम क्विज़ 4/8

अंतिम क्विज़

**Quiz:** p pointer हो तो *p क्या करता है?

- [ ] Gets its address
- [x] Accesses the value it points to
- [ ] Frees it
- [ ] Declares it

*Answer:* Accesses the value it points to. * pointer को dereference करता है।

## अंतिम क्विज़ 5/8

अंतिम क्विज़

**Quiz:** swap(int x, int y) caller के variables क्यों नहीं बदल पाता?

- [ ] C forbids swapping
- [ ] The compiler removes it
- [x] Arguments are copied
- [ ] It needs a header

*Answer:* Arguments are copied. मूल बदलने के लिए pointers पास करें।

## अंतिम क्विज़ 6/8

अंतिम क्विज़

**Quiz:** कौन-सा function heap पर memory allocate करता है?

- [ ] printf
- [ ] strlen
- [ ] fopen
- [x] malloc

*Answer:* malloc. हर malloc को एक free से जोड़ें।

## अंतिम क्विज़ 7/8

अंतिम क्विज़

**Quiz:** free() के तुरंत बाद pointer का क्या करें?

- [x] Set it to NULL
- [ ] Free it again
- [ ] Use it
- [ ] Cast it to int

*Answer:* Set it to NULL. इससे dangling-pointer उपयोग से बचाव होता है।

## अंतिम क्विज़ 8/8

अंतिम क्विज़

**Quiz:** Runtime पर buffer overflows कौन-सा tool पकड़ सकता है?

- [ ] make clean
- [x] AddressSanitizer
- [ ] ls
- [ ] diff

*Answer:* AddressSanitizer. -fsanitize=address के साथ compile करें।
