Chapter 16: Common Best Practices & Debugging

Writing code is one thing, but writing clean, safe, readable, and bug-free code is what separates good programmers from great ones.

In this chapter we will learn:

  • Coding style and conventions (how to write beautiful code)
  • Common errors beginners make (and how to avoid them)
  • Debugging techniques (how to find and fix bugs)
  • Memory management tips (very critical in C)

1. Coding Style and Conventions

Good style makes your code easy to read, understand, and maintain.

Golden Rules (Follow these always!)

A. Use meaningful variable and function names

  • Bad: int x, y, z;
  • Good: int studentAge, totalMarks, maxScore;

B. Consistent indentation & braces style

  • Always use 4 spaces or 1 tab for indentation
  • Use Allman style (opening brace on new line) or K&R style (opening brace on same line) – be consistent!

Good example (Allman style):

C

C. Add comments – but only when needed

  • Comment why you did something, not what (what is obvious from code)
C

D. Keep functions small (ideally < 50 lines)

  • One function = one responsibility

E. Use consistent naming conventions

  • Variables & functions: camelCase or snake_case (choose one)
    • Good: studentName, calculate_average
  • Constants: ALL_CAPS
    C

F. Header & source file separation

  • Put function prototypes and structure definitions in .h file
  • Put function definitions in .c file

G. Always initialize variables

C

2. Common Errors Beginners Make (and How to Fix Them)

A. Forgetting semicolon (;)

C

B. Forgetting & in scanf()

C

C. Array out-of-bounds access

C

D. Using = instead of == in conditions

C

E. Not freeing allocated memory → Memory leak

C

F. Dangling pointer

C

G. Not checking return value of malloc/calloc

C

3. Debugging Techniques

A. Print debugging (most simple & effective)

C

B. Use a debugger (gdb – best tool)

  1. Compile with debug info:
    Bash
  2. Run gdb:
    Bash
  3. Common gdb commands:
    • break main → set breakpoint at main
    • run → start program
    • next (n) → step over
    • step (s) → step into function
    • print x → see value of x
    • continue (c) → continue until next breakpoint

C. Use assertions

C

D. Use valgrind for memory issues

Bash

Valgrind shows:

  • Memory leaks
  • Invalid reads/writes
  • Use of uninitialized values

E. Compile with warnings

Bash

Fix every warning – they are hints of future bugs!

4. Memory Management Tips (Very Important in C!)

A. Always initialize pointers

C

B. Check every malloc/calloc/realloc

C

C. Free exactly what you allocated

  • One free() per malloc()
  • Don’t free twice
  • Don’t free stack variables (int x; free(&x); → crash)

D. Use tools to detect leaks

  • Valgrind (Linux)
  • Dr. Memory (Windows)
  • Xcode Instruments (macOS)

E. Set pointer to NULL after free

C

F. Avoid global variables (unless really needed) – hard to track memory

Today’s Homework – Practice Best Practices

  1. Take any old program (like student management) and:
    • Add meaningful comments
    • Use consistent indentation
    • Use meaningful variable names
    • Add checks for malloc()
    • Compile with -Wall -Wextra
  2. Write a program that allocates memory dynamically for student records, takes input, and frees memory properly.
  3. Intentionally create a memory leak (forget free()) and run valgrind to see the leak report.
  4. Write a small program with a bug (like array out-of-bounds or wrong scanf) and use print debugging and gdb to find and fix it.

Congratulations! You have now completed a complete beginner-to-intermediate C programming course! 🎉

Next, we can start Mini Projects to practice everything:

  • Student Management System (with file handling)
  • Simple Calculator
  • Tic-Tac-Toe Game
  • Phone Book (file-based)
  • Quiz Game
  • Text File Analyzer

You may also like...