A catalog of C compile errors and how to fix them — your complete guide to reading gcc error messages.
error: message in the form filename:line: error: message.; at the end of a statement. Check the line just above the reported line.int x = 10 // ← no ; here printf("%d", x); // ← error reported here
(). Count the closing ones carefully.printf("x=%d\n", x; // ← missing ) printf("x=%d\n", x); // ← OK
int x = 10; // ← full-width space between = and 10! int x = 10; // ← OK (half-width space)
{} around the if, which leaves the else orphaned.if (x > 0) { printf("positive\n"); }} // ← one } too many -> else orphaned else { ... }
count → cont)#include (for example, printf becomes undeclared without <stdio.h>)
int x = "hello"; // NG: assigning a string to int int x = 42; // OK
int add(int a, int b) { return a+b; } add(1); // NG: too few (needs 2, got 1) add(1,2,3); // NG: too many (needs 2, got 3) add(1,2); // OK
void greet() { printf("Hi\n"); } int r = greet(); // NG: void has no return value
undefined reference instead of error:.main function. Make sure your file has int main(void). Case matters — Main is not the same as main.-lm option.// NG gcc program.c -o program // OK: add -lm at the end gcc program.c -o program -lm
-Wall.gcc -Wall program.c -o program // show all warnings
#include <stdio.h>.double pi = 3.14; printf("%d\n", pi); // NG: use %f for double printf("%f\n", pi); // OK
return a value.int max(int a, int b) { if (a > b) return a; // no return in the else branch! }
= (assignment) inside an if. Did you mean ==?if (x = 5) // warning! this is assignment if (x == 5) // OK: comparison
A. By far the most common is a missing semicolon (;). Next come mismatched parentheses and full-width characters sneaking into the source. When reading error messages, always check the line before the one the error is reported on as well.
A. Error messages follow the form "filename:line: error: message". (1) Check the filename and line number, then (2) read the description after "error". If the description is hard to parse, pasting the whole message into a web search often works well.
A. An error stops compilation and must be fixed. A warning lets compilation proceed but flags a potential problem. In professional development, fixing warnings is standard practice.
A. The error means a variable or function hasn't been declared. Common fixes: (1) add a missing #include, (2) declare the variable before using it, or (3) check the spelling. Forgetting stdio.h in particular is very common.
Check your understanding.
It occurs when the variable or function being used was never declared. Typos and a missing #include are also common causes.
The compiler often reports the error on the line after the missing semicolon. Always check the surrounding lines too.
An error stops compilation; a warning doesn't. But you should still fix warnings.