{ }codelper

Error Messages › C

C expected ';' before

error: expected ‘;’ before ‘return’

The compiler is telling you that it was time for a ; but something else showed up. Most of the time you left out the semicolon at the end of a statement. How you read the message matters. The word after before is not where the semicolon is missing. It is the word that came right after that spot. So look just before that word, which is often the end of the line above.

Cause 1. No ; at the end of a statement

Without a ; after printf(...), the compiler assumes the statement is still going and reads on to the return on the next line. Only there does it notice something is wrong, which is why it says before 'return'. The ; mark in the output shows where to put it.

Wrong code

#include <stdio.h> int main(void) {    int a = 3;    printf("%d\n", a)    return 0;}
When you run it
main.c: In function ‘main’:
main.c:5:22: error: expected ‘;’ before ‘return’
    5 |     printf("%d\n", a)
      |                      ^
      |                      ;
    6 |     return 0;
      |     ~~~~~~

Edit and run it →

Fixed code

Put a ; at the marked spot.

#include <stdio.h> int main(void) {    int a = 3;    printf("%d\n", a);    return 0;}
Output
3

Edit and run it →

Cause 2. No ; at the end of a variable declaration

When you leave out the ; at the end of a declaration, the message is a little different. A declaration can also continue with , and another variable, so it says expected ',' or ';'. Here the line number in the error is not the line with the missing semicolon but the next line (line 5). The place to fix is the end of the line above it.

Sometimes one more error follows. int b = 4; could not be read properly, so you get an extra error saying b is unknown. Fix only the first error and compile again, and the rest go away with it.

Wrong code

#include <stdio.h> int main(void) {    int a = 3    int b = 4;    printf("%d\n", a + b);    return 0;}
When you run it
main.c: In function ‘main’:
main.c:5:5: error: expected ‘,’ or ‘;’ before ‘int’
    5 |     int b = 4;
      |     ^~~
main.c:6:24: error: ‘b’ undeclared (first use in this function)
    6 |     printf("%d\n", a + b);
      |                        ^
main.c:6:24: note: each undeclared identifier is reported only once for each function it appears in

Edit and run it →

Fixed code

Put a ; at the end of the line above the one the error points to.

#include <stdio.h> int main(void) {    int a = 3;    int b = 4;    printf("%d\n", a + b);    return 0;}
Output
7

Edit and run it →

Cause 3. Commas inside the for parentheses

The three parts inside the parentheses of a for are separated with ;, not ,. If you separate them with commas, the compiler reads all three parts as one piece and only reports the missing ; when it reaches the closing parenthesis. The warning before it comes from the same mistake.

Wrong code

#include <stdio.h> int main(void) {    int i;    for (i = 0, i < 3, i++) {        printf("%d\n", i);    }    return 0;}
When you run it
main.c: In function ‘main’:
main.c:5:15: warning: right-hand operand of comma expression has no effect [-Wunused-value]
    5 |     for (i = 0, i < 3, i++) {
      |               ^
main.c:5:27: error: expected ‘;’ before ‘)’ token
    5 |     for (i = 0, i < 3, i++) {
      |                           ^
      |                           ;
main.c:5:27: error: expected expression before ‘)’ token

Edit and run it →

Fixed code

Use two ; in the form for (start; condition; step).

#include <stdio.h> int main(void) {    int i;    for (i = 0; i < 3; i++) {        printf("%d\n", i);    }    return 0;}
Output
0
1
2

Edit and run it →

Cause 4. No ; after a struct definition

The } that ends a struct definition must be followed by a ;. It is easy to forget because the } of a function does not get one. In this case the error points not to the struct but to the line that comes after it (here, int main), which is confusing.

Wrong code

#include <stdio.h> struct Point {    int x;    int y;} int main(void) {    struct Point p = {1, 2};    printf("%d %d\n", p.x, p.y);    return 0;}
When you run it
main.c:8:1: error: expected ‘;’, identifier or ‘(’ before ‘int’
    8 | int main(void) {
      | ^~~

Edit and run it →

Fixed code

Put a ; after the closing brace of the struct.

#include <stdio.h> struct Point {    int x;    int y;}; int main(void) {    struct Point p = {1, 2};    printf("%d %d\n", p.x, p.y);    return 0;}
Output
1 2

Edit and run it →

What to check when you see this error

  1. Find the word after before and look just before it. If it is the first word on its line, look at the end of the line above.
  2. Even when there are several errors, fix only the first one and compile again. A single missing semicolon often causes a whole string of errors after it.
  3. Check that the inside of the for parentheses is split by two ;.
  4. Check that there is a ; after the } of the struct definition.
  5. The message differs between compilers. clang says expected ';' after expression, and Visual Studio says error C2143 (missing ';' before ...).

Practice problems

See also