Common C compiler errors and how to fix them

GCC's error messages are precise but not always easy to read as a beginner. Here are the errors and warnings you'll run into most often when using the C compiler, what they actually mean, and how to fix each one.

1. Missing semicolon

error: expected ';' before '}' token

This means a statement is missing its terminating semicolon — usually on the line just above where the error points.

int x = 5   // missing semicolon
printf("%d", x);

Fix: add the semicolon: int x = 5;

2. Undeclared variable

error: 'x' undeclared (first use in this function)

You used a variable before declaring its type, or misspelled its name.

x = 10;              // x was never declared
printf("%d", x);

Fix: declare the variable first: int x = 10;

3. Implicit declaration of a function

warning: implicit declaration of function 'sqrt'

You called a function without including the header file that declares it.

Fix: add the correct #include at the top of your file — for sqrt(), add #include <math.h>.

4. Segmentation fault (core dumped)

This isn't a compile error — it's a runtime crash. It means your program tried to access memory it isn't allowed to touch. Common causes:

int *ptr;
*ptr = 10;   // ptr was never assigned a valid address — crash

Fix: always initialize pointers before using them, and double-check array bounds in loops.

5. Expected declaration or statement at end of input

error: expected declaration or statement at end of input

Almost always means a missing closing curly brace } somewhere in the file — often a function or an if block that was never closed.

Fix: count your opening and closing braces carefully, or use the editor's bracket matching to spot the mismatch.

6. Format specifier mismatch

warning: format '%d' expects argument of type 'int', but argument has type 'double'

You used the wrong format specifier in printf() or scanf() for the variable's actual type.

float price = 9.99;
printf("%d", price);   // wrong — should be %f

Fix: match the specifier to the type — %d for int, %f for float/double, %c for char, %s for strings.

7. Conflicting types / redefinition

error: conflicting types for 'main'

Usually caused by declaring the same function or variable twice with different types, or a typo in a function signature.

Fix: check that each function and variable is declared exactly once with a consistent type.

8. Missing return statement

warning: control reaches end of non-void function

A function declared to return a value (like int) has a code path that doesn't actually return anything.

Fix: make sure every possible path through the function ends with a return statement matching its declared type.

Tip: read the first error first

When GCC reports several errors at once, fix the first one and recompile — later errors are often just side effects of the first mistake and disappear on their own once it's fixed. This is especially true for missing semicolons and unclosed braces, which can cascade into dozens of confusing follow-on errors.

← Test your fix in the C compiler