Arduino build error

error: expected unqualified-id before 'if'

Outside a function the compiler expects the name of something new: a variable, a function, a type. It found a statement or a keyword.

  • Build error
  • 3 common causes
  • Seen 2,761 times in failed builds on Mokxi in the 30 days to November 16, 2026.
The message

What the Arduino IDE prints

Output window
sketch.ino:3:1: error: expected unqualified-id before 'if'
    3 | if (count > 2) {
      | ^~
exit status 1

The usual reasons are a stray closing brace that ended a function early, a statement outside every function, and a reserved word used as a name.

Mokxi's compiler, clang, words it as expected unqualified-id, and often adds extraneous closing brace.

Causes

The 3 usual causes with the fix

1A statement outside every function

An if, a for or a call cannot stand alone at the top of the file. Move it into setup() or loop().

Broken
int count = 3;

if (count > 2) {
  count = 0;
}

void setup() {
  Serial.begin(9600);
  Serial.println(count);
}

void loop() {
}
Fixed
int count = 3;

void setup() {
  Serial.begin(9600);
  if (count > 2) {
    count = 0;
  }
  Serial.println(count);
}

void loop() {
}

Mokxi’s build tab says: This line is outside every function

Try this fix in Mokxi

2An extra closing brace at the end

One } too many closes the file early. Delete it, and let Auto Format show where the blocks really close.

Broken
void setup() {
  Serial.begin(9600);
  if (true) {
    Serial.println("a");
  }
  Serial.println("b");
}

void loop() {
}
}
Fixed
void setup() {
  Serial.begin(9600);
  if (true) {
    Serial.println("a");
  }
  Serial.println("b");
}

void loop() {
}

Mokxi’s build tab says: There is one } too many.

Try this fix in Mokxi

3A C++ keyword used as a name

Words such as new, class, delete and default are part of the language. A variable cannot use them.

Broken
int new = 5;

void setup() {
  Serial.begin(9600);
  Serial.println(new);
}

void loop() {
}
Fixed
int fresh = 5;

void setup() {
  Serial.begin(9600);
  Serial.println(fresh);
}

void loop() {
}

Mokxi’s build tab says: This line is outside every function

Try this fix in Mokxi

FAQ

Questions and answers

What does unqualified-id mean?

It means a plain name, such as a variable or function name. The compiler wanted one and found something else.

Why does an extra brace cause it?

The extra brace closes the function. The lines after it are outside any function, where statements are not allowed.

How do I find the extra brace?

Use Auto Format in the editor. The indentation then shows where a brace closes too early.