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.
Theory in plain words
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
Each wrong answer teaches a pattern you will meet in real firmware.
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 terminatesQuick quiz
1. How many bytes does "Ravi!" need?
Five letters plus the NUL terminator.
2. Safe overlap copy?
memmove handles overlapping regions.
Common questions
How many bytes does "Ravi!" need?
6. Five letters plus the NUL terminator.
Safe overlap copy?
memmove. memmove handles overlapping regions.