CC-ProgrammingBeginner-C-ProgrammingCommon-MistakesBest-PracticesDebugging

10 Common C Programming Mistakes & How to Fix Them

7.12 min read
Md Nasim SheikhMd Nasim Sheikh
Share:

Is your code crashing, printing garbage values, or just refusing to compile?

You are not alone. C is a powerful language, but it is also unforgiving. Unlike modern languages that hold your hand, C will happily let you write code that destroys your own memory or crashes your system.

This guide acts as a diagnostic checklist. If you are stuck, scan through these 10 common pitfalls to find the bug in your code.

Table of Contents

1. The Classic Syntax Errors (The "Silly" Mistakes)

These are the most annoying but easiest to fix. They usually prevent your code from compiling at all.

Missing Semicolons

Every statement in C must end with a semicolon. It is the period at the end of a sentence.

// ❌ WRONG
int x = 10 
printf("Value: %d", x);

// ✅ RIGHT
int x = 10; 
printf("Value: %d", x);

Mismatched Braces {}

If you get a "unexpected end of file" error, you probably forgot to close a function or an if block.

Pro Tip: Always indent your code. If your indentation looks wrong, your braces are likely wrong.

Advertisement

2. The "Off-By-One" Loop Error

This is the most common logical error in C. Remember: Arrays in C start at 0.

If you have an array of size 10, the valid indices are 0 through 9. If you try to access array[10], you are reading memory that doesn't belong to the array.

int arr[5]; // Creates indices 0, 1, 2, 3, 4

// ❌ WRONG: accessing index 5 (out of bounds)
for (int i = 0; i <= 5; i++) { 
    arr[i] = i; 
}

// ✅ RIGHT: stop strictly before 5
for (int i = 0; i < 5; i++) { 
    arr[i] = i;
}

3. Confusing Assignment = with Equality ==

This bug is dangerous because it usually compiles without error, but it breaks your logic completely.

  • = sets a value.
  • == compares values.
int x = 5;

// ❌ WRONG
// This sets x to 10, which evaluates to "true", so the code always runs.
if (x = 10) {
    printf("X is 10"); 
}

// ✅ RIGHT
if (x == 10) {
    printf("X is 10");
}

Advertisement

4. Uninitialized Variables

In many languages, variables default to 0. In C, they do not. An uninitialized variable contains whatever "garbage" random data happened to be in that memory address before.

// ❌ WRONG
int count; 
// 'count' could be 0, or it could be -32948122
printf("%d", count); 

// ✅ RIGHT
int count = 0;
printf("%d", count);

5. The "Dangling" Pointer (Memory Management)

Memory management is the hardest part of C. A "dangling pointer" occurs when you try to use memory after you have already freed it.

int *ptr = malloc(sizeof(int));
*ptr = 10;

free(ptr); // The memory is returned to the system

// ❌ WRONG: Accessing freed memory
// This causes undefined behavior (crashes or corruption)
*ptr = 20; 

// ✅ RIGHT
ptr = NULL; // Best practice: set to NULL after freeing

6. String Handling & The Missing Null Terminator

Strings in C are just character arrays that must end with a special null character (\0). If you forget this, functions like printf won't know where the string ends and will keep printing garbage memory until the program crashes.

// ❌ WRONG
char name[4] = {'J', 'o', 'h', 'n'}; 
// printf doesn't know where to stop!

// ✅ RIGHT (Manual)
char name[5] = {'J', 'o', 'h', 'n', '\0'};

// ✅ RIGHT (Automatic)
char name[] = "John"; // Compiler adds \0 automatically

Advertisement

7. Buffer Overflows (The Security Nightmare)

This happens when you try to put more data into a container than it can hold. This is a common security vulnerability.

char buffer[10];

// ❌ WRONG
// "Hello World" is 11 chars + 1 null terminator = 12 chars
strcpy(buffer, "Hello World"); 

// ✅ RIGHT
// Use functions that limit the size
strncpy(buffer, "Hello World", sizeof(buffer) - 1);

8. Integer Division Surprises

In C, dividing two integers results in an integer. Any decimal points are strictly truncated (not rounded).

// ❌ WRONG
float result = 5 / 2; 
// Logic: 5/2 is 2 (integer). Result becomes 2.0.

// ✅ RIGHT
float result = 5.0 / 2.0; 
// Logic: 5.0/2.0 is 2.5. Result becomes 2.5.

9. Scanning Strings with scanf

scanf is tricky. A common mistake for beginners is including the & (address-of operator) when scanning a string. Since a string variable name is already a pointer to the first character, you don't need the &.

char name[50];
int age;

// ❌ WRONG for strings
scanf("%s", &name); 

// ✅ RIGHT
scanf("%s", name);  // No & for strings
scanf("%d", &age);  // & is required for int

10. Forgetting return in Non-Void Functions

If you promise the compiler your function will return an int, you must return one. If you forget, your program might seemingly work on your machine but crash on another.

// ❌ WRONG
int add(int a, int b) {
    int sum = a + b;
    // Missing return!
}

// ✅ RIGHT
int add(int a, int b) {
    return a + b;
}

Summary: The "Clean Code" Checklist

Before you run your code, do a quick pass:

  1. Are all sentences ended with ;?
  2. Did you initialize your variables (int x = 0;)?
  3. Are your loops stopping at < size and not <=?
  4. Did you confuse = with == inside an if statement?
  5. If you used malloc, did you use free?

Debugging is the best way to learn C. Don't be discouraged by errors; every syntax error you fix makes you a better programmer!


Advertisement

Where to go next?

Test Your Skills
Advanced Level

Algorithms & Problem Solving

Test your knowledge and reinforce what you've learned in this article.

20/100
Questions
25 min
Duration
Start Quiz
Md Nasim Sheikh
Written by

Md Nasim Sheikh

Software Developer at softexForge

Verified Author150+ Projects
Published:

You May Also Like