error types in C, compile time error, runtime error, logical error, debugging C programs, C warnings, segmentation fault, divide by zero, debug tips in C, Tamil Technicians C course,

Error Types in C – Compile Time, Runtime, Logical Errors & Debugging Tips 2025

Error Types in C – Compile Time, Runtime, Logical Errors & Debugging Tips | Tamil Technicians

Tamil Technicians – C Programming Course · Error Types & Debugging

Error Types in C – Compile Time, Runtime, Logical Errors & Debugging Tips

Every C programmer – from beginner to expert – faces errors and bugs. Errors are not a sign of failure; they are feedback from the compiler or from the program itself.

In this lesson, you’ll learn:

  • The main types of errors in C: compile-time, runtime, logical (and warnings).
  • How to read and understand compiler error messages.
  • Common real-world examples: missing semicolon, divide by zero, array out of bounds, wrong formulas.
  • Practical debugging techniques: printf debugging, breaking problems into smaller parts, using a debugger, tracing values step by step.
  • A simple, repeatable debugging checklist for students and technicians.
Error types in C – compile time, runtime and logical errors with debugging flow for C programs
Figure: Different error types in C (compile time, runtime, logical) and a debugging flow to trace and fix issues.

1. Why Understanding Error Types Matters

When you build a circuit and it doesn’t work, you use a multimeter, check voltages, and trace signals. Similarly, when a C program fails, you need to know:

  • Where the problem is: during compilation, during execution, or in the formula/logic.
  • What kind of error it is: syntax, missing file, wrong pointer, infinite loop, etc.
  • How to systematically debug and fix it without guessing randomly.
Big picture

Most C errors fall into three main groups:

  1. Compile-time errors – caught by the compiler before the program is run.
  2. Runtime errors – occur while the program is running.
  3. Logical errors – the program runs, but gives wrong output.

2. Compile-Time Errors (Syntax & Semantic Errors)

Compile-time errors are reported by the compiler (e.g., GCC) when it tries to translate your .c file into machine code. If compile-time errors exist, the compiler usually does not generate an executable.

Type

Syntax Errors

Violating C grammar rules: missing semicolons, wrong braces, invalid keywords.

Type

Type Errors

Incompatible data types, wrong arguments to functions, invalid pointer conversions.

Type

Missing Declarations

Using variables or functions without proper declaration or header file.

2.1 Syntax error examples

#include <stdio.h>

int main() {
    int a = 10
    printf("a = %d\n", a);
    return 0;
}

Here, the line int a = 10 is missing a semicolon. Compiler message (approx):

error: expected ';' before 'printf'

2.2 Type mismatch example

#include <stdio.h>

int main() {
    int a = 10;
    float b = 5.5f;

    printf("Sum = %d\n", a + b);   // wrong format specifier
    return 0;
}

Many compilers show a warning here:

warning: format '%d' expects argument of type 'int',
but argument 2 has type 'double'
Treat warnings seriously. Many bugs come from ignoring compiler warnings (especially format-string and type-related warnings).

2.3 Undeclared identifier example

#include <stdio.h>

int main() {
    total = 10;   // 'total' never declared
    printf("%d\n", total);
    return 0;
}

Compiler error:

error: 'total' undeclared (first use in this function)
Tip: Read compiler errors from the top. Often the first error is the root cause; many others are side effects.

3. Runtime Errors – Crashes & Abnormal Behaviour

Runtime errors happen when the program is running. The code compiles successfully, but something goes wrong while executing: divide by zero, invalid memory access, file not found, etc.

Example

Divide-by-zero

Division where denominator becomes zero due to user input or logic.

Example

Segmentation fault

Invalid memory access (null pointer, out-of-bounds array index).

Example

File errors

Trying to read from or write to a file that cannot be opened.

3.1 Divide-by-zero example

#include <stdio.h>

int main() {
    int energy, units;
    printf("Enter total energy and units: ");
    scanf("%d %d", &energy, &units);

    int perUnit = energy / units;   // danger if units == 0
    printf("Per unit = %d\n", perUnit);

    return 0;
}

If the user enters units = 0, the program may crash or show undefined behaviour.

Always validate user input or data before using it in divisions, array indices or pointer arithmetic.

3.2 Array out-of-bounds example

#include <stdio.h>

int main() {
    int a[5];
    int i;

    for (i = 0; i <= 5; i++) {  // error: should be i < 5
        a[i] = i * 10;
    }

    return 0;
}

The loop writes to a[5], which is outside the valid range (0–4). Sometimes this causes a crash (segmentation fault), sometimes it silently corrupts memory, producing strange bugs later.

3.3 Null pointer dereference example

#include <stdio.h>

int main() {
    int *ptr = NULL;
    printf("%d\n", *ptr);  // dereferencing NULL -> runtime crash
    return 0;
}

Accessing memory through a NULL pointer is a classic runtime error.

4. Logical Errors – Program Runs but Output Is Wrong

Logical errors are the most dangerous ones because:

  • The program compiles successfully (no compile-time error).
  • The program runs without crashing (no runtime error).
  • The output is wrong – your calculation or decision logic is incorrect.

4.1 Wrong formula example – average

#include <stdio.h>

int main() {
    int a = 10, b = 20, c = 30;
    float avg;

    avg = a + b + c / 3;   // logical error in formula
    printf("Average = %.2f\n", avg);

    return 0;
}

According to C’s precedence rules, this is:

a + b + (c / 3)

instead of:

(a + b + c) / 3
Use parentheses to make formulas explicit and avoid precedence-related logical errors.

4.2 Off-by-one error in loops

#include <stdio.h>

int main() {
    int i;
    for (i = 1; i <= 10; i++) {
        printf("%d ", i);
    }
    // Suppose we wanted to print only 1 to 9 – loop condition is off by one.
    return 0;
}

Off-by-one errors appear very often in loops and array indexing.

4.3 Wrong comparison operator

if (voltage = 230) {   // assignment, not comparison
    printf("Voltage is 230V\n");
}

This assigns 230 to voltage instead of comparing. The condition becomes true for all cases. The correct version is:

if (voltage == 230) {
    ...
}

5. Warnings vs Errors – Why Warnings Matter

Compilers usually distinguish between:

  • Errors – program will not compile.
  • Warnings – program compiles, but compiler suspects a bug.
Make it a habit to aim for zero warnings. Many serious bugs start as simple warnings like:
  • “Format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘float’”.
  • “Control reaches end of non-void function”.
  • “Variable ‘x’ is used uninitialized”.

6. How to Read Compiler Error Messages

When the compiler prints an error, it usually gives:

  • File name (e.g., main.c).
  • Line number (e.g., line 12).
  • Column or position.
  • A short description of the problem.

Example error message

main.c:12:5: error: expected ';' before 'return'

Interpretation:

  • File: main.c
  • Line: 12, column: 5
  • Problem: the compiler was expecting a semicolon before the keyword return.
Debug habit
  • Always fix errors from top to bottom.
  • After fixing a few errors, recompile – don’t wait to fix everything in one shot.
  • Read the entire message; sometimes the key hint is at the end.

7. Debugging Tips – Step-by-Step Approach

Debugging is a process, not magic. Here are practical techniques that work in real C programs.

7.1 Use printf as a “software multimeter”

Just like you probe voltages, you can probe variable values using printf:

printf("DEBUG: i = %d, sum = %d\n", i, sum);

Insert such statements around suspicious logic to see:

  • Whether loops are running the expected number of times.
  • How variables change step by step.
  • Where the program “jumps” into unexpected branches.

7.2 Reduce the problem – create a small test program

If a bug appears in a large project:

  1. Copy the related function / logic into a small separate .c file.
  2. Hard-code some test data.
  3. Run only that part until the bug is understood.

7.3 Check boundaries and assumptions

  • Array loops: verify start and end index. Are you going from 0 to n-1 or 1 to n?
  • Division: ensure denominator is never 0.
  • Pointers: make sure they are initialized before use; check for NULL.
  • File handles: always check if fopen returned NULL before reading/writing.

7.4 Use a debugger (gdb / IDE debugger)

Many IDEs (Code::Blocks, VS Code with extensions, etc.) let you:

  • Set breakpoints where the program will pause.
  • Step through code line by line (step, next).
  • Inspect variable values and call stack.
Even if you start with printf-based debugging, gradually practicing with a real debugger will make you much more efficient.

7.5 Don’t change too many things at once

When debugging:

  • Change one small thing, recompile, test.
  • If the bug is still there, undo or adjust and try a different change.
  • Keep notes of what you tried and what happened.

8. Debugging Checklist – For Every C Program

  • I read the first compiler error carefully and fix it before worrying about the rest.
  • I treat warnings seriously and try to keep my program warning-free.
  • I validate user input (especially for divisions and array sizes).
  • I check array bounds and loop limits to avoid off-by-one errors.
  • I test formulas with simple known values to catch logical errors early.
  • I use printf or a debugger to trace variable values step by step.
  • I avoid making many random changes at once; I debug systematically.
  • I understand the difference between compile-time, runtime and logical errors.

FAQ: Error Types & Debugging in C

1. What is the main difference between compile-time and runtime errors?

Compile-time errors are detected by the compiler before the program runs, usually due to syntax or type rule violations. The code will not compile until you fix them. Runtime errors occur while the program is executing, even though it compiled successfully. They often involve invalid memory access, divide-by-zero, or file access problems.

2. Why are logical errors harder to find?

Logical errors do not produce compiler errors or obvious crashes. The program appears to work, but it produces incorrect results because the algorithm or formula is wrong. Finding them requires testing with known values, tracing intermediate results and carefully reviewing the logic.

3. Is it okay to ignore compiler warnings if the program runs?

No. Many serious bugs (especially in C) are hinted by warnings: type mismatches, possible uninitialized variables, or suspicious format strings. Aim to reduce warnings to zero for safer and more maintainable code.

4. How can beginners get better at debugging C programs?

Practice a consistent debugging approach: read errors from top to bottom, add temporary printf statements to inspect variable values, test small pieces of code individually, and gradually learn to use a debugger (gdb or an IDE debugger). Over time, patterns will become familiar and you’ll recognize common mistakes quickly.

Category: C Programming Course · Lesson – Error Types in C (Compile Time, Runtime, Logical & Debugging Tips)


Buy link ;

Acer Aspire Lite, AMD Ryzen 7-7730U, 16 GB RAM, 1TB SSD, Full HD IPS, 15.6″/39.62 cm, Windows 11 Home, MS Office, Steel Gray, 1.59 KG, AL15-41, Thin and Light Laptop.


Contact link ;

Click Here >>>>>>


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top