← Back to C Course | Chapter 12: Advanced Topics | Lesson 19 of 20

C Common Mistakes

Missing Semicolons

Every executable C statement must end with a semicolon, and omitting even one usually produces a compiler error pointing at the following line rather than the actual missing semicolon, which can make the real cause of the error confusing to track down at first.

Example: Missing Semicolons

c
#include <stdio.h>
int main() {
	int x = 5;
	printf("%d", x);
	return 0;
}

Uninitialized Variables

Unlike some languages, C does not automatically zero-initialize local variables, so a freshly declared variable holds whatever leftover bit pattern happened to already be sitting in that stack memory. Reading it before explicitly assigning a value produces unpredictable, garbage results.

Example: Uninitialized Variables

c
#include <stdio.h>
int main() {
	int x = 0;
	printf("%d", x);
	return 0;
}

Missing Ampersand (&) in scanf

scanf() needs the memory address of the variable it's writing into, not the variable's value, so a call like scanf("%d", age); (missing the &) writes to whatever random address happens to be stored in age, often crashing the program instead of reading the intended input.

Example: Missing Ampersand (&) in scanf

c
#include <stdio.h>
int main() {
	int age;
	sscanf("25", "%d", &age);
	printf("%d", age);
	return 0;
}

Array Index Out of Bounds

C performs no automatic bounds checking on array access, so reading or writing past an array's declared size doesn't raise an error — it silently accesses whatever memory happens to sit adjacent to the array, which can corrupt unrelated variables or crash the program unpredictably.

Example: Array Index Out of Bounds

c
#include <stdio.h>
int main() {
	int arr[5] = {1,2,3,4,5};
	printf("%d", arr[4]);
	return 0;
}

Infinite Loop Triggers

A loop only terminates once its condition becomes false, so forgetting to update the loop's controlling variable inside the loop body (like forgetting i++ in a for/while loop) leaves that condition permanently true and the loop runs forever.

Example: Infinite Loop Triggers

c
#include <stdio.h>
int main() {
	int i = 0;
	while (i < 5) {
		printf("%d ", i);
		i++;
	}
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.