A catalog of C compile errors and how to fix them. A 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 parens.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 if, so else ends up orphaned.if (x > 0) { printf("positive\n"); }} // ← one } too many -> else orphaned else { ... }
count → cont)#include (e.g. 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. Check that your file has int main(void). Case matters (Main is NOT 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.int max(int a, int b) { if (a > b) return a; // no return in the else branch! }
= (assignment) in an if. Did you mean ==?if (x = 5) // warning! this is assignment if (x == 5) // OK: comparison
A. By far the most common cause is a missing semicolon (;). Next is mismatched parentheses, then full-width characters sneaking in. When reading error messages, always check "the line before the one the error is reported on" as well.
A. Error messages have the form "filename:line: error: message". (1) Check the filename and line, (2) read the description after "error". If the description is hard, copying the whole message into a web search is also effective.
A. An error stops compilation and must be fixed. A warning still compiles but flags a latent problem. In professional development, fixing warnings is standard practice.
A. The error means a variable or function has not been declared. Fixes: (1) add a #include, (2) declare the variable first, (3) check spelling. Forgetting stdio.h in particular is very common.
Check your understanding!
It occurs when a variable or function being used was not declared. Typos and missing #include are also common causes.
The compiler often reports the error on the line after the missing semicolon. Always check the lines around the error too.
An error fails compilation, a warning does not. But you should still fix warnings.