Common Mistakes in C Programming: 16 Errors With Exact Fixes
Every C programmer hits the same wall of mistakes. The good news: most of them produce specific, recognizable symptoms — a specific compiler error, crash pattern, or wrong output. This guide organizes the 16 most common mistakes into four categories, gives you the exact error message or symptom, shows the broken code and the fix, and tells you which compiler flag or tool catches it.
int main() {
printf("Hi\n"); // undeclared
return 0;
}#include <stdio.h>
int main() {
printf("Hi\n");
return 0;
}void main() is not valid C. The OS and shell scripts need main()'s return value as the process exit code. Shell scripts test it with echo $?; CI systems use it to detect failures.void main() {
printf("Hi\n");
}int main() {
printf("Hi\n");
return 0;
}int x = 10 // ↠missing ;
return 0;int x = 10;
return 0;{ needs a matching }. GCC reports the error at the end of the file — far from the actual mistake. Use an editor with bracket matching (VS Code highlights the mismatched brace; Vim's % key jumps between pairs).int main() {
if (1) {
printf("hi\n");
// ↠missing }
return 0;
}int main() {
if (1) {
printf("hi\n");
}
return 0;
}if (x > 0)
printf("pos\n");
printf("always!\n"); // ALWAYS runsif (x > 0) {
printf("pos\n");
printf("also pos\n");
}if (x = 5) assigns 5 to x and evaluates to true (5 is non-zero), so the body always runs. Compiles without error. GCC -Wall warns. Yoda conditions if (5 == x) make a typo a compile error.if (x = 5) { // assigns, not compares
printf("always\n");
}if (x == 5) {
printf("when x is 5\n");
}i <= n instead of i < n accesses arr[n] — out-of-bounds, undefined behavior. One too many iterations; or using i < n-1 may miss the last element.int arr[5] = {1,2,3,4,5};
for (int i = 0; i <= 5; i++) // accesses arr[5]!
printf("%d\n", arr[i]);int arr[5] = {1,2,3,4,5};
for (int i = 0; i < 5; i++) // 0..4
printf("%d\n", arr[i]);for (int i=0; i<10; i++); // empty body!
printf("%d\n", i); // runs once after loopfor (int i=0; i<10; i++) {
printf("%d\n", i);
}int sum;
for (int i=1; i<=5; i++)
sum += i; // sum starts as garbageint sum = 0;
for (int i=1; i<=5; i++)
sum += i;malloc returns NULL when the system is out of memory. Using the returned pointer without checking it causes a null pointer dereference — an immediate crash. Always validate before use.int *p = malloc(1000 * sizeof(int));
p[0] = 42; // crash if NULLint *p = malloc(1000 * sizeof(int));
if (!p) { perror("malloc"); return 1; }
p[0] = 42;char *buf = malloc(256);
use(buf);
// free(buf) missingchar *buf = malloc(256);
use(buf);
free(buf);
buf = NULL;strncpy, snprintf, fgets.char buf[8];
strcpy(buf, "Hello, World!"); // overflow!char buf[8];
strncpy(buf, "Hello, World!", sizeof(buf)-1);
buf[sizeof(buf)-1] = '\0';free(ptr), the memory may be reused for another allocation. Reading or writing through the freed pointer reads/writes someone else's data — a serious bug and common security vulnerability. Setting the pointer to NULL makes subsequent accesses crash immediately (caught) rather than silently corrupting memory (uncaught).int *p = malloc(sizeof(int));
free(p);
*p = 42; // undefined behavior!int *p = malloc(sizeof(int));
free(p);
p = NULL; // next use crashes immediatelymalloc, fopen, or scanf, all of which can return NULL.FILE *fp = fopen("missing.txt", "r");
fread(buf, 1, 100, fp); // crash if NULLFILE *fp = fopen("file.txt", "r");
if (!fp) { perror("fopen"); return 1; }
fread(buf, 1, 100, fp);int a = 2147483647; // INT_MAX
int b = a + 1; // undefined behavior!
printf("%d\n", b);long long a = 2147483647LL;
long long b = a + 1; // 2147483648
printf("%lld\n", b);int fact(int n) {
return n * fact(n - 1); // infinite!
}int fact(int n) {
if (n <= 1) return 1;
return n * fact(n - 1);
}Tools: Find These Mistakes Automatically
| Tool / Flag | What it finds | How to use |
|---|---|---|
| gcc -Wall -Wextra | Implicit declarations, unused vars, format mismatches, assignment in condition, missing returns | gcc -Wall -Wextra prog.c -o prog |
| gcc -fsanitize=address | Buffer overflows, use-after-free, double-free, stack overflows, memory leaks | gcc -g -fsanitize=address prog.c -o prog && ./prog |
| gcc -fsanitize=undefined | Signed integer overflow, null pointer dereference, out-of-bounds indexing | gcc -g -fsanitize=undefined prog.c -o prog && ./prog |
| valgrind | Memory leaks, use-after-free, uninitialized reads, invalid reads/writes | gcc -g prog.c -o prog && valgrind --leak-check=full ./prog |
| gdb | Pinpoints segfault line, shows call stack at crash, step-through debugging | gcc -g prog.c -o prog && gdb ./prog → run, bt |
| clang --analyze | Null pointer dereference, dead code, memory leaks — static, no execution needed | clang --analyze prog.c |
gcc -Wall -Wextra -Werror -g -fsanitize=address -fsanitize=undefined -std=c11 prog.c -o prog && ./progThis single command catches all four categories of mistakes simultaneously.
Frequently Asked Questions
#include for functions, (2) omitting braces around if/for/while bodies, (3) using uninitialized variables, (4) not checking malloc/fopen return values, and (5) array off-by-one errors. All five are caught by compiling with -Wall -fsanitize=address.gcc -g -fsanitize=address prog.c -o prog && ./prog — AddressSanitizer prints the exact line and cause.= instead of == in an if condition (always true), missing braces so only one statement is conditional, off-by-one loop bounds, uninitialized variable (garbage value), or integer overflow. Compile with -Wall -fsanitize=undefined to catch most of these.-fsanitize=undefined.valgrind --leak-check=full ./prog — exhaustive, reports exact allocation sites, ~10–20x slower. (2) gcc -fsanitize=address prog.c -o prog && ./prog — AddressSanitizer, ~2x overhead, reports leaks at exit with source locations. Use ASan for daily development; valgrind for final pre-release checks.Continue learning C on CoodeVerse
Frequently asked questions
What are the most common mistakes in C programming?
The most common C programming mistakes: (1) Structure — missing #include, void main(), missing semicolons, mismatched braces. (2) Logic — missing braces around if/for bodies, using = instead of == in conditions, off-by-one array errors. (3) Memory — not checking malloc return value, forgetting free, buffer overflows. (4) Runtime — NULL pointer dereference, integer overflow, infinite recursion.
What causes segmentation fault in C?
Segfault in C occurs when the program accesses invalid memory: dereferencing a NULL pointer (fopen/malloc returned NULL but wasn't checked), array out-of-bounds access, use-after-free, or stack overflow from infinite recursion. Diagnose with: gcc -g -fsanitize=address prog.c -o prog && ./prog
Why does my C program compile but give wrong output?
Common causes: using = instead of == in an if condition (always true), missing braces so only one statement is in the if-body, off-by-one loop error, reading an uninitialized variable (garbage value), or integer overflow. Compile with -Wall -fsanitize=undefined to catch most of these.
What is undefined behavior in C?
Undefined behavior means the C standard makes no guarantee — the program might crash, produce wrong output, or appear to work but fail after a compiler update. Common causes: signed integer overflow, reading uninitialized variables, NULL dereference, array out-of-bounds, use-after-free. Catch with -fsanitize=undefined.
How do I find memory leaks in C?
Two tools: (1) valgrind --leak-check=full ./prog — exhaustive, reports exact allocation sites. (2) gcc -fsanitize=address prog.c -o prog && ./prog — AddressSanitizer, ~2x overhead, reports leaks at exit. Use ASan for daily dev; valgrind for final checks.
What is an off-by-one error in C?
Off-by-one occurs when a loop iterates one too many or too few times. Arrays are 0-indexed — a 5-element array has indices 0..4. Using i <= n instead of i < n accesses arr[n] which is out of bounds — undefined behavior. Always use: for (int i = 0; i < n; i++) to access all n elements.
What is the = vs == bug in C?
if (x = 5) assigns 5 to x and evaluates to true (non-zero), so the body always runs regardless of x's original value. It compiles silently. GCC -Wall warns: 'suggest parentheses around assignment used as truth value'. Fix: use == for comparison. Yoda conditions (if (5 == x)) make the typo a compile error.