🇯🇵 日本語 | 🇺🇸 English

Lesson 33: Compile Errors Dictionary

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

📖 What you'll learn on this page
✅ Must-know essentials
  • Read the first error message first
  • Missing semicolons and brackets are the most common cause
  • Type mismatches and missing declarations
⭐ Read if you have time
  • "undefined reference" (linker errors)
  • Shadow warnings
  • Forgetting to include a header

Syntax Errors — The #1 Cause of Failed Compiles

When gcc spots a syntax mistake, it prints an error: message in the form filename:line: error: message.
error: expected ';' before '}' token
Cause: A 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 itself.
error: expected ')' before ';' token
Cause: Mismatched parentheses (). Count the closing ones carefully.
printf("x=%d\n", x;  // ← missing )
printf("x=%d\n", x);  // ← OK
error: stray '\343' in program
Cause: Full-width characters (full-width spaces or parentheses) have slipped in. They're hard to spot by eye, which makes them nasty bugs.
int x = 10; // ← full-width space between = and 10!
int x = 10;  // ← OK (half-width space)
If you see stray '\343', it's almost always a full-width character. Enable "show full-width spaces" in your editor.
error: 'else' without a previous 'if'
Cause: Mismatched {} around the if, which leaves the else orphaned.
if (x > 0) {
  printf("positive\n");
}} // ← one } too many -> else orphaned
else { ... }

Type Errors — Declarations and Type Mismatches

Errors caused by missing declarations or type mismatches.
error: 'x' undeclared (first use in this function)
Common causes:
(1) The variable was never declared
(2) The name is misspelled (countcont)
(3) The variable is used outside its scope (referenced from outside the block where it's declared)
(4) A missing #include (for example, printf becomes undeclared without <stdio.h>)
error: incompatible types when assigning to type 'int' from type 'char *'
Cause: The types on either side of the assignment are incompatible.
int x = "hello"; // NG: assigning a string to int
int x = 42;       // OK
error: too few / too many arguments to function 'foo'
Cause: The number of arguments at the call site doesn't 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: You're trying to assign 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 Compiles, But It's Risky

Warnings don't stop compilation, but they often flag hidden bugs. Turn them all on 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: The printf format specifier doesn't match the argument type.
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. Either remove it or check whether you meant to use it somewhere.
warning: control reaches end of non-void function
Cause: A non-void function has a code path that doesn't return a value.
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) inside an if. Did you mean ==?
if (x = 5)  // warning! this is assignment
if (x == 5) // OK: comparison

Error-Decoding Quiz

Read the gcc error message and try to identify the cause.
Question
← Previous lesson
Lesson 32: Sorting Algorithms
Next lesson →
Lesson 34: Runtime Errors & Segfault

Related lessons

Loops, Arrays, Strings
Lesson 17: Debugging Techniques
How to debug C programs. Practical coverage of printf debugging and common bugs.
Reference
Lesson 34: 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 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.

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

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.

Q. What is the difference between error and warning?

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.

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

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.

Review 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 the variable or function being used was never declared. Typos and a 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 surrounding lines too.

Q3. Difference between warning and error?

No difference
Compilation succeeds despite warnings
Warnings are more serious

An error stops compilation; a warning doesn't. But you should still fix warnings.

Share this article
Share on X Share on Facebook