Source: https://mokxi.com/errors/expected-unqualified-id
Updated: 2026-11-16

Home / Errors
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.

Try the fixed sketch in Mokxi See the causes

- 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

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

Fixed

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

Fixed

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

Fixed

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.

Next

## More errors and the longer guide

- error: expected '}' at end of input
- error: a function-definition is not allowed here before '{' token

The longer guide to this error All Arduino errors
