🇯🇵 日本語 | 🇺🇸 English
Advertisement

Lesson 31: Compile Errors Dictionary

A catalog of C compile errors and how to fix them. A complete guide to reading gcc error messages.

Syntax Errors — The #1 Cause of Failed Compiles

When gcc finds a syntax mistake it emits an error: message in the form filename:line: error: message.
error: expected ';' before '}' token
Cause: Missing semicolon ; 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
Rule of thumb: always check the line before the error line, not just the error line!
error: expected ')' before ';' token
Cause: Mismatched parentheses (). Count the closing parens.
printf("x=%d\n", x;  // ← missing )
printf("x=%d\n", x);  // ← OK
error: stray '\343' in program
Cause: Full-width characters (full-width spaces, full-width parentheses) have slipped in. They are hard to see — nasty.
int x = 10; // ← full-width space between = and 10!
int x = 10;  // ← OK (half-width space)
If you see stray '\343' it is almost always a full-width character. Turn on "show full-width spaces" in your editor!
error: 'else' without a previous 'if'
Cause: Mismatched {} around if, so else ends up orphaned.
if (x > 0) {
  printf("positive\n");
}} // ← one } too many -> else orphaned
else { ... }

Type Errors — Declarations and Type Mismatches

Errors about missing declarations and type mismatches.
error: 'x' undeclared (first use in this function)
Common causes:
(1) Forgot to declare the variable
(2) Misspelled name (countcont)
(3) Used outside its scope (referencing a variable declared inside a block from outside)
(4) Missing #include (e.g. printf becomes undeclared without <stdio.h>)
error: incompatible types when assigning to type 'int' from type 'char *'
Cause: Incompatible types in an assignment.
int x = "hello"; // NG: assigning a string to int
int x = 42;       // OK
error: too few / too many arguments to function 'foo'
Cause: Call site argument count does not match the declaration.
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
error: void value not ignored as it ought to be
Cause: Assigning the "result" of a void-returning function to a variable.
void greet() { printf("Hi\n"); }
int r = greet(); // NG: void has no return value

Warnings — It Runs But It's Risky

Warnings allow compilation to succeed, but they often point to latent bugs. Enable them all with -Wall.
gcc -Wall program.c -o program  // show all warnings
warning: implicit declaration of function 'printf'
Cause: Missing #include<stdio.h>.
warning: format '%d' expects type 'int', but argument 2 has type 'double'
Cause: printf format specifier and argument types do not match.
double pi = 3.14;
printf("%d\n", pi);  // NG: use %f for double
printf("%f\n", pi);  // OK
warning: unused variable 'x'
Cause: A declared variable is never used. Remove it if unused, or look for a missed use.
warning: control reaches end of non-void function
Cause: A non-void function has a path that does not return.
int max(int a, int b) {
  if (a > b) return a;
  // no return in the else branch!
}
warning: suggest parentheses around assignment used as truth value
Cause: You used = (assignment) in an if. Did you mean ==?
if (x = 5)  // warning! this is assignment
if (x == 5) // OK: comparison
Advertisement

Related lessons

Loops, Arrays, Strings
Lesson 15: Debugging Techniques
How to debug C programs. Practical coverage of printf debugging and common bugs.
Reference
Lesson 32: Runtime Errors and Segfaults
Causes of segmentation faults in C and how to fix them.

FAQ

Q. What is the most common cause of compile errors?

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.

Q. I don't understand error messages. What should I look at?

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.

Q. What is the difference between error and warning?

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.

Q. How do I fix an "undeclared identifier" error?

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.

Quiz

Check your understanding!

Q1. What causes an "undeclared identifier" error?

The variable was not declared
A semicolon is missing
The type is wrong

It occurs when a variable or function being used was not declared. Typos and missing #include are also common causes.

Q2. Where is a missing-semicolon error typically reported?

On the line that is missing the semicolon
On the next line (often offset by one)
At the top of the file

The compiler often reports the error on the line after the missing semicolon. Always check the lines around the error too.

Q3. Difference between warning and error?

No difference
Compilation succeeds despite warnings
Warnings are more serious

An error fails compilation, a warning does not. But you should still fix warnings.

Share this article
Share on X Share on Facebook