Home / Learn Chip Programming / Top C Mistakes in Firmware (and How to Avoid Them)

Top C Mistakes in Firmware (and How to Avoid Them)

The bugs that waste most embedded hours. Learn them once, avoid them for life.

In shortThe bugs that waste most embedded hours. Learn them once, avoid them for life.

Theory in plain words

Ten classic bugs1 Buffer too small by one (no space for \0)2 sizeof(ptr) is pointer size, not array size3 Compare strings with strcmp not ==4 Return address of a local variable5 Forget break in switch6 Signed vs unsigned comparison7 memcpy on overlapping buffers: use memmove8 Missing volatile for ISR variables9 Using freed memory10 Integer overflow in size calculations
Ten classic bugs

What it is

Off-by-one, missing string terminator, signed/unsigned confusion, sizeof on a pointer, returning pointer to a local, missing break, memcpy overlap, == on strings, malloc/free mismatch, forgetting volatile.

Why we use it

Memory bugs corrupt data silently and crash far from the cause. These ten cause most field failures.

How it works

Know each pattern, enable warnings, use sanitizers on the PC, and review with a checklist.

Where it is used

  • Daily coding
  • Code reviews and interviews
  • Debugging crashes

Bug hunt quiz

Find the bugTry it Yourself

Each wrong answer teaches a pattern you will meet in real firmware.

Safe patterns
char buf[16];
snprintf(buf, sizeof buf, "%s", name);        // never strcpy / sprintf into fixed buffers
memmove(dst, src, n);                          // overlap safe; memcpy is not
if (strcmp(cmd, "start") == 0) run();          // compare text, not pointers
for (size_t i = n; i-- > 0; ) { }              // unsigned reverse loop that terminates

Quick quiz

1. How many bytes does "Ravi!" need?

2. Safe overlap copy?

Common questions

How many bytes does "Ravi!" need?

6. Five letters plus the NUL terminator.

Safe overlap copy?

memmove. memmove handles overlapping regions.