Learn

Fixing "expected '}' at end of input" in Arduino

Beginner

Or see every lesson.

  • 505parts on the bench
  • 45boards running now
  • 1.00xreal time, on every board
Arduino Uno: blink, ready to breaklive0.000 s 0.00x
Click to open it in the editor
The LED on pin 13 blinks once a second. Delete the last } of the sketch and press Run: the build stops with expected '}' and offers to add it back.

expected '}' at end of input means the compiler reached the end of the sketch while a { was still open. Every { needs a } to close it, and one of them never got its }. Most of the time it is the last brace of loop(), lost while editing, or the brace of an if, an else or a for inside it. A comment that starts with /* and never ends hides the closing brace too, and so does a class or a struct left open.

The Arduino IDE compiles with GCC, which says expected '}' at end of input and, in GCC 13, adds a note, to match this '{', at the brace it gave up on. Mokxi compiles with clang, which says expected '}' with the same note. The circuit above is the Uno blink. Open it, delete the last } of the sketch and press Run to see the error and the button that fixes it.

The last brace of loop()

Delete the final } of the blink sketch and clang says expected '}' on line 19, the empty line after the end of the file, with a note, to match this '{', at line 14, the { of void loop(). GCC 13 gives expected '}' at end of input for the same sketch, and the same note. The error is at the end because that is where the compiler noticed. The note is the useful part: it names the brace that is still open.

Mokxi’s build tab explains it in plain words: "The { on line 14 is never closed. Add the matching } where that block should end: if it is a function, after its last line, and check that no other } above it is missing." It also has a button, Add the }, which puts one at the end of the file. On the blink sketch that is the right place, and the sketch built and blinked again.

A brace missing inside the function

The note does not always point at the brace that is really missing. In the sketch below, the else on line 11 is never closed. The compiler pairs each } with the nearest open {, so the } on line 15, meant for loop(), closes the else, and the one left open is the { of loop() itself. Clang said expected '}' just after that brace, on line 15, with the note at line 8, and GCC 13 pointed its note at the same brace.

So when the note points at a function, read down through that function and check each if, else, for, while and switch for its own }. Here the fix button builds, but it puts the brace at the very end, so delay(5); ends up inside the else. The right fix is a } after digitalWrite(LED_BUILTIN, LOW);.

Broken: an else that is never closed
const int BUTTON_PIN = 2;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    digitalWrite(LED_BUILTIN, HIGH);
  } else {
    digitalWrite(LED_BUILTIN, LOW);

  delay(5);
}

Open this sketch in the editorThe code alone: the editor places the board and the parts it can read from the sketch.

Finding the open brace with Auto Format

Auto Format, Ctrl+T (Cmd+T on a Mac) in the Arduino IDE and in Mokxi’s code editor, indents each line by how many braces are open around it. Run it and look for the place where the indenting stops making sense: in the sketch above, delay(5); stays pushed in under the else, which is the clue that the else never ended. A function that starts indented, instead of at the left edge, means the one above it is still open.

Counting works too. Every function’s braces should add up to zero: one { after its first line, one } at its end, and a pair around every block in between. Count down from the { the note names, adding one for each { and taking one away for each }, and the line where you expected to reach zero and did not is where the brace is missing.

A /* comment that never ends

A comment that starts with /* runs until */, however many lines that takes. Comment out a few lines with /* and forget the */, and the closing brace of the function becomes part of the comment. Clang said unterminated /* comment at the /*, and then expected '}' at the end of the file. GCC 13 said unterminated comment, then expected '}' at end of input.

Fix the first error and the second goes with it: add the */, or use // at the start of each line you want to switch off, which can never run past its own line.

A class, a struct or a switch left open

The same message comes from any block, not just functions. A class whose last member function is missing its } swallows setup() and loop(): clang said expected '}' at the end, then expected ';' after class. A switch inside loop() with no } of its own gave expected '}' with the note at the { of loop(), as the else did.

If you pasted code from a forum or a tutorial, check that you got all of it. A copy that stops a line short of the end is a common way to lose the last brace.

If the error is somewhere else

A missing } in setup() often shows up first as a different message, at the next function: clang says function definition is not allowed here and GCC a function-definition is not allowed here before '{' token, because void loop() now starts inside setup(). The end-of-input error follows it. That case has its own guide, linked below. Fix the first error in the list first; the rest often go away with it.

Open this circuit in the editor. Arduino Uno: blink, ready to break, wired as it runs at the top of this page. Change a part or a value and run it again.

Open the circuit

Questions

What does "expected '}' at end of input" mean?

The sketch ended while a { was still open. Some function, if, else, loop, class or struct is missing its closing }. The note under the error names the brace that is still open.

Where do I put the missing }?

Where the block should end. If the note points at loop() and every block inside it is closed, the } goes at the end of the sketch. If a block inside is open, close that block instead, right after its last line.

Why does the note point at the wrong brace?

Braces pair with the nearest open one. If an if or an else inside loop() is missing its }, the } meant for loop() closes it, and loop()’s own { is the one left open. Check the blocks inside the function the note names.

Does Mokxi show the same error?

Mokxi compiles with clang, which says expected '}' without "at end of input", with the same kind of note. Its build tab says which line’s { is never closed and has a button that adds a } at the end of the file.

Build this for real

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