/errors/c/expected-semicolon-before
gcc error: expected ';' before
The commands on this page have not been executed by our verification runner yet.updated
- tool
- C
- exit code
- 1
- versions
- GCC 4+, Clang (different wording)
- os
- linux, macos, windows
error: expected ';' before 'X' means GCC reached token X while the previous statement was still open: a semicolon is missing just before it. The compiler only notices at the next token, so look at the end of the statement before X, which is often on the previous line. Add the semicolon there, or after the } of a struct or enum definition. Clang reports the same problem as error: expected ';' after expression.
Example
#include <stdio.h>
int main(void) {
int total = 0
printf("%d\n", total);
return 0;
}GCC reports error: expected ';' before 'printf': it names the token it did not expect. Depending on the GCC version, the location shown is either the end of line 4 (with a fix-it hint that shows the missing ;) or the start of printf on line 5. Either way the fix is a ; after int total = 0.
Checklist
| Where | What is usually missing |
|---|---|
| End of the previous line | The semicolon of a declaration, assignment or function call |
| After a closing brace | struct, union, enum or typedef definitions need }; |
| do { ... } while (cond) | The ; after while (cond) |
| Inside a header | A prototype or declaration without ; (the error appears in the .c file after the #include) |
| A type name before the token | A missing #include, so the type is unknown and parsing goes wrong |
Sources
Related pages
errors · cC error: implicit declaration of function 'foo' [-Wimplicit-function-declaration]C compilers reject a call to a function with no prior declaration; GCC 14 and Clang 16 made it an error. Include the right header or add a prototype first.errors · cppC++ undefined reference to `function()' (ld returned 1 exit status)undefined reference is a linker error: a function was declared but its definition was not linked. Add the .cpp file or library, fix -l order, or define it.flags · cgcc -Wall -Wextra -Werror: enable compiler warningsgcc -Wall -Wextra enables the warnings that catch most C bugs: unused variables, format mismatches, signed comparisons. -Werror makes warnings fail the build.