Common C Errors & How to Fix Them

    Atul Kabra3 min readUpdated

    Most C errors beginners hit fall into a handful of categories: compile errors (the code won't build), warnings (it builds but something's suspicious), and runtime errors like segmentation faults (it builds but crashes). The fix is almost always faster once you can name the category. Here are the common ones and how to solve each.

    Compile errors

    Missing semicolon. C ends statements with ;. A missing one usually shows an error on the next line, which confuses beginners. Check the line above the reported error.

    int x = 5    /* ERROR: missing ; -- the compiler complains on the next line */
    printf("%d", x);
    

    Undeclared identifier. You used a variable or function before declaring it, or forgot to #include its header (e.g. using printf without #include <stdio.h>). Declare the variable, or add the header.

    Mismatched braces or parentheses. Every { needs a }. Indent consistently and your editor's bracket matching will show the gap.

    Warnings worth fixing

    Always compile with warnings enabled (-Wall). Warnings often mark real bugs.

    = used where == was meant. This is the single most famous C bug:

    int x = 0;
    if (x = 5) {        /* assigns 5, condition is always true */
        /* always runs -- almost never what you wanted */
    }
    /* FIX: */
    if (x == 5) { /* ... */ }   /* compares */
    

    Format specifier mismatch. Printing the wrong type prints garbage:

    double price = 9.99;
    printf("%d\n", price);   /* WRONG: %d is for int */
    printf("%f\n", price);   /* FIX: %f for double in printf */
    

    See Variables & Data Types for the full specifier table.

    Want to learn this properly?

    Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.

    Explore C Programming

    Runtime errors: the segmentation fault

    A segmentation fault ("segfault") means your program touched memory it shouldn't. The build succeeded, but it crashes when run. The usual causes:

    #include <stdio.h>
    
    int main(void) {
        int *p = NULL;
        printf("%d\n", *p);   /* CRASH: dereferencing a NULL pointer */
        return 0;
    }
    

    Fixes for the common segfault causes:

    • NULL or uninitialized pointer — initialize pointers and check != NULL before dereferencing.
    • Array out of bounds — an array of size 5 has indices 0..4; a[5] is invalid.
    • Missing & in scanfscanf("%d", x) should be scanf("%d", &x).
    • Writing past a string buffer — leave room for the '\0'; see Strings in C.
    • Use-after-free — don't touch memory after free; see Dynamic Memory.

    A debugging checklist

    1. Read the first error, not the last — later errors are often knock-on effects.
    2. Enable warnings (-Wall -Wextra) and treat them seriously.
    3. Check the line above a "missing semicolon" or "expected" error.
    4. Print intermediate values to see where reality diverges from your expectation.
    5. For crashes, suspect pointers and array indices first — they cause most segfaults.

    Common mistakes (quick reference)

    • = vs == in conditions.
    • Wrong printf/scanf format specifier for the type.
    • Forgetting & in scanf.
    • Off-by-one array indexing causing out-of-bounds access.
    • Dereferencing NULL or uninitialized pointers.
    • Forgetting to free (leak) or using memory after freeing it.

    FAQ

    Why does the error point to the wrong line? C often detects a problem (like a missing ; or }) only when it reaches the next token. Look just above.

    What does "segmentation fault (core dumped)" mean? Your program accessed invalid memory. Inspect every pointer and array index near the crash.

    Continue at the hub C Programming, then Pointers in C and Arrays in C.

    Want to learn this properly? Join the waitlist for our C Programming course — taught in Jalgaon.

    Want to learn this properly?

    Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.

    Explore C Programming
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides