You build your program and GCC stops with this:
semi_return.c:4:19: error: expected ';' before 'return'
GCC was reading your code. Then it hit something it didn't expect and quit. Here's the catch. It tells you where it noticed the problem, and that isn't always the line where you made the mistake. Below you'll see how to read these messages and the seven mistakes behind most of them. I tested every example with GCC 16.1 on Windows (MinGW-w64).
How to Read the Message
Take semi_return.c:4:19: error: expected ';' before 'return'. The file is semi_return.c. The line is 4 and the column is 19. After error: GCC tells you what it wanted (';') and what it got instead ('return'). That last quoted word is the token it was looking at when it gave up.
Two habits make these errors much easier. First, look at the line above the one GCC names. A missing symbol usually gets noticed one token too late. Second, fix only the first error and rebuild. One mistake can cause several messages, and the later ones often vanish once the first is gone.
The Seven Mistakes
The line numbers below match the code as you see it here. The files on GitHub start with a one-line comment, so every number there is one higher.
1. A Missing Semicolon After a Statement
#include <stdio.h>
int main(void) {
printf("hi\n")
return 0;
}
semi_return.c:4:19: error: expected ';' before 'return'
The printf on line 4 has no semicolon. GCC flags the end of that line (column 19), because the next thing it met was return. End the statement and you're done:
#include <stdio.h>
int main(void) {
printf("hi\n");
return 0;
}
2. A Missing Semicolon After a Declaration
#include <stdio.h>
int main(void) {
int x = 5
printf("%d\n", x);
return 0;
}
semi.c:5:5: error: expected ',' or ';' before 'printf'
It's the same mistake, but GCC points at a different spot. The semicolon is missing on line 4 (int x = 5). The error shows up on line 5. Why? After a declaration GCC keeps reading, in case you wanted to declare another variable (,). It only gives up when the next statement starts. So if the named line looks fine, check the one above it.
#include <stdio.h>
int main(void) {
int x = 5;
printf("%d\n", x);
return 0;
}
3. A Missing Closing Parenthesis
#include <stdio.h>
int main(void) {
int x = 5;
if (x > 3 {
printf("big\n");
}
return 0;
}
paren.c:5:14: error: expected ')' before '{' token
paren.c:9:1: error: expected expression before '}' token
The condition (x > 3 never closes. GCC only notices when it runs into the {. The second message is just fallout from the first. That's why you fix one error at a time.
#include <stdio.h>
int main(void) {
int x = 5;
if (x > 3) {
printf("big\n");
}
return 0;
}
4. A Missing Closing Brace
#include <stdio.h>
int main(void) {
printf("hi\n");
return 0;
brace.c:5:5: error: expected declaration or statement at end of input
main opens with a { and nothing ever closes it. GCC reaches the end of the file still waiting. The line it names is the last one in the file, so it says nothing about where the brace belongs. Hunt for a block you opened and forgot to close. Your editor’s bracket matching will help.
#include <stdio.h>
int main(void) {
printf("hi\n");
return 0;
}
5. Curly Quotes
#include <stdio.h>
int main(void) {
printf(“hello”);
return 0;
}
quotes.c:4:12: error: stray '\342' in program
quotes.c:4:18: error: stray '\342' in program
quotes.c:4:13: error: 'hello' undeclared (first use in this function); did you mean 'ftello'?
The quotes around hello look normal. They aren't. They're the curly quotes “ and ”, not the plain " on your keyboard. They tend to sneak in when you copy code from a web page, a chat or a word processor. GCC can't read them, and \342 is the first byte of one. After that it treats hello as a variable name, so the last message calls it undeclared. Retype the quotes by hand:
#include <stdio.h>
int main(void) {
printf("hello\n");
return 0;
}
6. A Struct Without a Semicolon
#include <stdio.h>
struct Point {
int x;
int y;
}
int main(void) {
struct Point p = {1, 2};
printf("%d\n", p.x);
return 0;
}
struct_semi.c:8:1: error: expected ';', identifier or '(' before 'int'
A struct definition is a statement, so the closing brace needs a semicolon. GCC points at whatever it sees next, the int in int main. The real mistake is earlier:
#include <stdio.h>
struct Point {
int x;
int y;
};
int main(void) {
struct Point p = {1, 2};
printf("%d\n", p.x);
return 0;
}
7. An Unterminated String
#include <stdio.h>
int main(void) {
printf("hello);
return 0;
}
string.c:4:12: error: missing terminating " character
string.c:5:5: error: expected expression before 'return'
The string opens at the first " and never ends. GCC swallows the rest of the line, closing parenthesis and semicolon included. The second message is more fallout. Close the string and it goes away:
#include <stdio.h>
int main(void) {
printf("hello\n");
return 0;
}
Which Message Points at Which Mistake?
| GCC says | Look for |
|---|---|
expected ';' before 'return' (or another statement) |
A missing ; at the end of the line above |
expected ',' or ';' before ... |
A missing ; after a declaration, on the line above |
expected ')' before '{' token |
A parenthesis that was opened and never closed |
expected declaration or statement at end of input |
A { that was never closed |
stray '\342' in program |
Curly quotes or another non-keyboard character pasted in |
expected ';', identifier or '(' before ... after a }
|
A struct or enum definition missing its ;
|
missing terminating " character |
A string with no closing quote |
The Rule, Stated Once
A syntax error means GCC couldn't make sense of what you wrote. It reports where it gave up, and that isn't always where you slipped. So check the line above the one it names. Fix only the first error. Then rebuild.
Seeing implicit declaration of function instead? Read C Error: Implicit Declaration of Function. Does the program build, but the linker complains? Try gcc: Undefined Reference to WinMain. Does it build and then crash? Go to Segmentation Fault in C: Find the Exact Line. Just starting out with C? Begin with Introduction to C programming and User Input in C: A Complete Guide for Beginners.
Download the code: you'll find the broken and fixed versions of all seven on GitHub in C-Programing/Troubleshooting/syntax-errors. Compile one file at a time, for example gcc -Wall -Wextra bug.c -o bug.exe.
Top comments (0)