C Programming - Beginner to Brilliance / Common Errors and Debugging

Introduction to Errors & Types

🧠 What is an "Error"?

An error is any mistake in the program that prevents it from running correctly or giving the desired output.

Errors can happen:

  • While writing the code (e.g., forgetting a semicolon),

  • When the code is being compiled,

  • When the program is being linked,

  • Or even during execution (after the program starts running).


πŸ§ͺ Why Are Errors Important?

Errors are the signals from the compiler or runtime system telling you something is wrong:

  • They protect your program from crashing

  • They help you spot mistakes early

  • They force good coding habits like checking inputs, handling exceptions, etc.


πŸ”Ž Classification of Errors in C

Let’s break them down one by one with simple examples.

1️⃣ Syntax Errors (Compile-Time Errors)

πŸ“ These are grammar mistakes in your C code β€” violations of the syntax rules defined by the C language.

β›” The compiler will not compile your code until you fix them.

βœ… Examples:

int main() {
  printf("Hello, world!")  // ❌ Missing semicolon
}

βœ”οΈ Fix:

printf("Hello, world!");

🧠 Other common syntax errors:

  • Misspelled keywords (e.g., pritnf instead of printf)

  • Mismatched parentheses or braces

  • Using undeclared variables


2️⃣ Semantic Errors

🧠 The code is syntactically correct, but it doesn't make logical sense according to the rules of C.

β›” Compiler may not complain, but your program behaves incorrectly.

βœ… Example:

int x = "hello";  // ❌ Assigning string to an integer

βœ”οΈ Fix:

char *x = "hello";

3️⃣ Logical Errors

πŸ’‘ These are thinking mistakes β€” your logic is flawed.

β›” The code compiles and runs, but gives wrong output.

βœ… Example:

int a = 5, b = 10;
printf("Average: %d", a + b / 2); // ❌ wrong precedence

Expected: Average of 5 and 10 = 7

Actual Output: 5 + (10 / 2) = 5 + 5 = 10

βœ”οΈ Fix:

printf("Average: %d", (a + b) / 2);

4️⃣ Runtime Errors (During Execution)

πŸ’£ These happen after the program starts running.

β›” Common with memory problems, invalid input, or math errors.

βœ… Examples:

  • Divide by zero:

    int x = 10 / 0;  // ❌
  • Invalid memory access:

    int *p = NULL;
    *p = 100;        // ❌ segmentation fault
  • Array out-of-bound:

    int arr[3];
    arr[10] = 5;     // ❌ Invalid index

5️⃣ Linker Errors

🧱 After compilation, the linker tries to connect your code with required libraries and symbols.

β›” These errors occur when the linker can’t find a function, variable, or symbol that’s used in your code.

βœ… Example:

void greet();  // Declared but never defined

int main() {
  greet();  // ❌ Undefined reference to greet
}

βœ”οΈ Fix:

void greet() {
  printf("Hello!");
}

6️⃣ Compilation Warnings

⚠️ These are not errors β€” they don't stop your program from compiling, but they warn you about potential issues that may cause bugs or crashes.

βœ… Examples:

  • Variable declared but never used

  • Possible loss of data in type conversion

  • Uninitialized variable usage

int x;
printf("%d", x);  // ⚠️ 'x' is used uninitialized

πŸ“Š Summary Table

Error Type

When It Occurs

Example

Impact

Syntax Error

Compile-time

Missing semicolon

Stops compilation

Semantic Error

Compile-time

Wrong type assignment

Compilation fails

Logical Error

Runtime/Logic

Wrong formula, condition

Wrong output

Runtime Error

Execution

Divide by zero, invalid pointer

Program crashes

Linking Error

Linking phase

Function not defined

Program won't build

Warnings

Compile-time

Type mismatch, uninitialized vars

May lead to errors later


🧠 Tips to Handle Errors as a Beginner

πŸ”Ή Don't panic when you see red text β€” errors are helpful.

πŸ”Ή Read the full error message, especially the line number.

πŸ”Ή Fix one error at a time.

πŸ”Ή Use tools like gcc -Wall to enable more warnings.

πŸ”Ή Use print statements or gdb to track down bugs.