Expected Semicolon in C: Common Compiler Errors & Solutions
The "error: expected ‘;’ before..." diagnostic is triggered when the GCC syntax parser encounters a new statement, variable declaration, or closing brace without finding the required statement terminator (;) on the preceding statement. Because C is a free-form language that ignores newlines, the compiler often flags the line AFTER the actual mistake.
Quick Fix Summary
Check the line immediately preceding the reported error line. Pay special attention to struct declarations (struct Node { ... };), typedefs, and multiline macro definitions.

Why This Error Happens
The Parser Lookahead Paradox: Why Compilers Point to the Wrong Line
Because C treats whitespace, line breaks, and indentation as optional separators, the compiler cannot know that your previous statement ended where your line break happened. The parser continues reading forward, greedily trying to combine tokens, until it hits an invalid token that cannot syntactically follow. Only at that moment does it raise expected ; before.... Consequently, whenever GCC reports a missing semicolon on line 20, look immediately at line 19 or line 18.
The Classic Struct & Union Definition Trap
Unlike function blocks which end simply with a closing curly brace }, struct and union definitions MUST terminate with a semicolon: struct Node { int val; };. Forgetting the trailing semicolon confuses the parser into believing that whatever function or variable declaration follows immediately is actually an instance variable of that struct, causing bizarre and misleading syntax errors downstream.
Top Root Causes & Specific Code Fixes
1. Struct or Union Definition Missing Trailing Semicolon
Unlike functions, struct definitions require a semicolon after the closing curly brace `}`.
struct Point {
int x;
int y;
} // BUG: Missing semicolon here!
int main(void) { ... }struct Point {
int x;
int y;
}; // Fixed: Trailing semicolon present
int main(void) { ... }2. Premature Semicolon on Loop / If Header
Placing a semicolon immediately after `for (...)` or `if (...)` creates an empty statement, causing the following block to run unconditionally or scoping variables out.
for (int i = 0; i < 5; i++); // BUG: Empty loop body!
{
printf("%d\n", i); // Error: 'i' undeclared
}for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}3. Missing Semicolon in Macro Expansions
If a macro definition `#define` includes or omits a semicolon incorrectly, expanding it inside an `if-else` chain can break semicolon expectations.
#define LOG(msg) printf(msg);;
if (condition) LOG("Test") else do_something(); // Error on else// Use do { ... } while(0) idiom for safe multi-statement macros
#define LOG(msg) do { printf(msg); } while(0)Complete Broken and Corrected Programs
Compare the source before running it. The broken program is intentionally incorrect; its behavior can depend on the compiler and platform.
Read the broken program
#include <stdio.h>
// BUG 1: Missing semicolon after struct definition
struct UserProfile {
int id;
char username[32];
} // Missing semicolon here causes error on next line
int calculate_tax(int income) {
int rate = 15 // BUG 2: Missing semicolon on variable declaration
return income * rate / 100;
}
int main(void) {
struct UserProfile user = {1, "Alice Chen"};
printf("User ID: %d, Name: %s\n", user.id, user.username);
printf("Tax: %d\n", calculate_tax(50000));
return 0;
}Open broken program in compilerRead the corrected program
#include <stdio.h>
// FIXED 1: Semicolon added after struct definition
struct UserProfile {
int id;
char username[32];
};
int calculate_tax(int income) {
// FIXED 2: Semicolon added after variable assignment
int rate = 15;
return income * rate / 100;
}
int main(void) {
printf("=== Expected Semicolon Resolved ===\n");
struct UserProfile user = {1, "Alice Chen"};
printf("User ID: %d, Name: %s\n", user.id, user.username);
printf("Calculated Tax: $%d\n", calculate_tax(50000));
return 0;
}Open corrected program in compilerExpected output from the corrected program
Reference output, not a live execution result.
=== Expected Semicolon Resolved ===
User ID: 1, Name: Alice Chen
Calculated Tax: $7500Debugging Techniques and Commands
Inspect line (N - 1) before the reported error line
The reported token can follow the actual mistake. Inspect the preceding statements as well as the flagged line.
gcc -Wall -Wextra main.c
Enables additional GCC warnings that flag empty statements and dubious semicolon placement in control headers.
Frequently Asked Questions
Why does C require a semicolon after a struct declaration but not a function?
A struct definition can be immediately followed by variable instantiations (e.g. `struct Point { int x, y; } p1, p2;`). The semicolon indicates to the compiler that no variables are being declared.