Learn

Arduino "expected ';' before" and the other "expected" errors

Intermediate

Or see every lesson.

  • 455parts on the bench
  • 43boards running now
  • 1.00xreal time, on every board
Arduino Uno: a traffic light, ready to breaklive0.000 s 0.00x
Hold the button
A working traffic light. Open it, delete a semicolon or a bracket, and press Run to see which "expected" error you get.

Every "expected" error means the same thing: the compiler was reading your sketch, met a word or a symbol that cannot come next, and is telling you what it was waiting for instead. The symbol after "before" is where it stopped, which is usually just after the real mistake, not on it.

This page takes each "expected" message the Arduino IDE prints, in GCC’s words, with what clang, the compiler Mokxi uses, says about the same line, and the fix. The circuit above is a traffic light whose sketch has plenty of semicolons, braces and brackets to remove. Open it, delete one, press Run, and read the error.

"expected ';' before"

GCC names the token after the gap. pinMode(13, OUTPUT) with no semicolon, followed by the closing brace of setup(), gives expected ';' before '}' token on the line of the brace. Clang points at the gap itself: expected ';' after expression, with a caret just after OUTPUT). Either way, the semicolon goes at the end of the line before the one GCC names.

Outside a function the wording changes. A global such as int count = 0 with no semicolon, followed by void setup(), gives expected ',' or ';' before 'void' from GCC and expected ';' after top level declarator from clang. Same fix.

Lines that open a block take no semicolon: void setup() {, if (x > 5) {, for (...) {. A semicolon there is a different and quieter bug: if (x > 512); { ends the if at the semicolon, so the block below it runs every time. It compiles without a word in both compilers, so when an if seems to be ignored, look at the end of its line.

Semicolons after statements and declarations, none after a line that opens a block
int count = 0;

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  count = count + 1;
  if (count % 2 == 0) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }
  Serial.println(count);
  delay(500);
}

"expected ')' before"

A round bracket opened and never closed. if (analogRead(A0) > 512 { opens two and closes one, so GCC says expected ')' before '{' token and clang says expected ')', with a note pointing at the bracket it could not match. Count the pairs from the inside out: every ( needs its ) before the {.

A for loop written with commas instead of semicolons, for (int i = 0, i < 3, i++), is a cousin of this one. Clang says expected ';' in 'for' statement specifier; GCC’s wording depends on its version. The three parts of a for are separated by semicolons.

"expected primary-expression before"

Something is missing in the middle of an expression. if (x > ) { has nothing after the >, so GCC says expected primary-expression before ')' token and clang says expected expression. A primary expression is a value: a number, a variable, a string, a function call.

The same message comes from writing a type where a value should be, as in Serial.println(int);, or from passing an array with its brackets, sum(values[]), where the call wants just sum(values).

"expected initializer before"

A declaration that stops halfway. int count 0; leaves out the =, so GCC says expected initializer before numeric constant, and clang says expected ';' after top level declarator, pointing just after count. Put the = back between the name and the value.

GCC says the same thing, with a different word after "before", when a function’s first line has a stray word in it. In void blink(int times) fast { the compiler expects the function’s opening brace straight after the closing bracket, so GCC reports expected initializer before 'fast' and clang reports expected function body after function declarator. Read the line from the left: a type, a name, the brackets with the parameters, then the brace.

"expected unqualified-id" and "expected declaration before '}'"

These come from code that has ended up outside every function, usually because a brace closed one too soon. An extra } at the end of loop() leaves the next } with nothing to close: GCC says expected declaration before '}' token, clang says extraneous closing brace ('}').

If the extra brace is in the middle of loop(), the lines after it are outside every function, and an if or a for there gives GCC’s expected unqualified-id before 'if' (clang: expected unqualified-id). Find the brace that closes the function early. Auto Format, Ctrl+T in the Arduino IDE, makes it stand out by pulling the rest of the function out to the left margin.

"stray '\342' in program"

This one is not punctuation you forgot but punctuation you pasted. Code copied from a web page, a PDF or a word processor can bring curly quotes (“ ” ‘ ’), a long dash in place of a minus sign, or an invisible non-breaking space. GCC reports each byte of those characters on its own, as stray '\342' in program, stray '\200' in program and so on, several times a line. Clang names the character: unexpected character <U+201C>.

Retype the quotes and minus signs on that line by hand, or paste the code into a plain text editor first. A line that is all strays is almost always a copied one.

Questions

What does "expected ';' before" mean in Arduino?

A statement is missing its semicolon. The symbol after "before" is the start of the next statement, so the semicolon goes at the end of the line before it.

What does "stray '\342' in program" mean?

Your sketch has a character from outside plain text, nearly always a curly quote, a long dash or a non-breaking space pasted from a web page or a document. Retype the quotes and dashes on that line.

What is a primary-expression?

A value the compiler can use on its own: a number, a variable name, a string or a function call. "expected primary-expression" means one is missing where an operator or a bracket needed it.

Does Mokxi show the same errors as the Arduino IDE?

The same mistakes, in different words. The Arduino IDE uses GCC and Mokxi uses clang; this page gives both, so you can search for either.

Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.