Learn

Fixing "expected primary-expression before" in Arduino

Beginner

Or see every lesson.

  • 455parts on the bench
  • 43boards running now
  • 1.00xreal time, on every board
Arduino Uno: read a knob, ready to breaklive0.000 s 0.00x
Click to open it in the editor
It prints the knob’s reading five times a second. Delete 1023L from the millivolts line and press Run: the build stops with expected expression.

expected primary-expression before ')' token means the compiler needed a value at that point of an expression and found something that is not one. A primary expression is the simplest kind of value: a number, a variable, a string in quotes, or a function call. Either a value is missing, as in if (x > ), or something that is not a value is where one should be, such as a type name, an empty pair of brackets or the word return.

The word in quotes after "before" is what the compiler found instead, so the mistake is just in front of it. Mokxi compiles with clang, which words most of these expected expression. The circuit above reads a knob on A0 and prints "A0 = 512 (2502 mV)" with the knob in the middle. Open it, delete the 1023L from the millivolts line and press Run to see the error.

A value missing after an operator or a comma

The commonest cause: an operator or a comma with nothing after it. GCC names what came next. if (raw > ) and analogWrite(9, ); both gave expected primary-expression before ')' token, analogWrite(, 128); gave before ',' token, int level = ; gave before ';' token, and while () gave before ')' token again. Clang said expected expression for each, and Mokxi’s build tab says "Something is missing here: a value, a bracket or a name the line needs before it can go on."

The demo’s break is one of these. With 1023L deleted, the line reads long millivolts = (long)raw * 5000L / ; and clang stops with expected expression at the semicolon. Put the value back.

firmware/uno/examples/analog (the program running above, comments trimmed)
const int SENSOR_PIN = A0;

void setup() {
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: analogRead on A0");
}

void loop() {
  int raw = analogRead(SENSOR_PIN);
  long millivolts = (long)raw * 5000L / 1023L;

  Serial.print("A0 = ");
  Serial.print(raw);
  Serial.print("  (");
  Serial.print(millivolts);
  Serial.println(" mV)");

  delay(200);
}

=> for >=, and a range written like math

if (raw => 512) has its two symbols the wrong way around. C++ reads = and then > with nothing between them, so GCC says expected primary-expression before '>' token and clang expected expression. The operator is >=, and the same goes for <=.

if (raw > 100 && < 900) reads well in English and not in C++: each side of && is its own comparison and needs its own value. GCC said expected primary-expression before '<' token. Write the variable twice: if (raw > 100 && raw < 900).

"before 'int'", "before 'char'" and "before 'const'": a type inside a call

Types belong in declarations, int raw = analogRead(A0);, and in a function’s own first line, void blinkLed(int times). They do not belong where the function is called. Serial.println(int raw); gave GCC’s expected primary-expression before 'int', blinkLed(int 3); the same, Serial.println(char c); gave before 'char', and Serial.println(const int x = 5); gave before 'const'.

Clang words the first three differently: expected '(' for function-style cast or type construction, because int(raw) with brackets would have been a conversion. Delete the type and pass the value: Serial.println(raw);, blinkLed(3);.

"before ']' token": an array passed with its brackets

A function that takes an array is written with brackets, int total(int values[], int count), and that makes it tempting to call it the same way. total(readings[], 3) gave GCC’s expected primary-expression before ']' token and clang’s expected expression at line 10. When you pass the whole array, pass its name alone. The sketch below printed 12.

An array passed by its name
int total(int values[], int count) {
  int sum = 0;
  for (int i = 0; i < count; i++) sum += values[i];
  return sum;
}

void setup() {
  Serial.begin(9600);
  int readings[] = {3, 4, 5};
  Serial.println(total(readings, 3));
}

void loop() {
}

"before 'return'": return inside an expression

return is a statement, not a value, so it cannot sit inside ? :. digitalRead(pin) == LOW ? return 1 : return 0; gave GCC’s expected primary-expression before 'return' twice and expected ':' before 'return' between them. Clang said expected expression, then expected ':' with a note pointing back at the ?: to match this '?'. Put return first and let the ? : choose the value: return digitalRead(pin) == LOW ? 1 : 0;.

"before '.' token": a type name with a dot

String.length() asks the type for its length, not your text. GCC said expected primary-expression before '.' token. Clang said 'String' does not refer to a value, and the build tab explains: "String is the name of a type (a class), not of a variable, so it cannot be used as one." Use the variable’s name: with String name = "Mokxi";, Serial.println(name.length()); printed 5.

A semicolon before else

A ; after the } of an if ends the if there, so the else on the next line has nothing to belong to. Clang said expected expression at the else. GCC words this one differently: 'else' without a previous 'if'. Delete the semicolon between } and else.

An if with no braces fails the same way when it is given a second line. Without braces only the first line after the if belongs to it, so in the sketch below delay(10); runs every time and ends the if, and the else after it has no if left. Clang warned first, misleading indentation; statement is not part of the previous 'if', then stopped with expected expression at the else on line 10; GCC again says else without a previous if. Indenting a line does not put it inside an if: braces do.

Broken: two lines under an if with no braces
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  int raw = analogRead(A0);
  if (raw > 512)
    digitalWrite(13, HIGH);
    delay(10);
  else
    digitalWrite(13, LOW);
}

Questions

What does "expected primary-expression before" mean in Arduino?

A value is missing, or something that is not a value is where one should be. A primary expression is a number, a variable, a string or a function call. The word after "before" is what the compiler found in its place.

How do I fix "expected primary-expression before ')' token"?

Something is missing just before the closing bracket: a value after an operator, as in if (x > ), or after a comma, as in analogWrite(9, ). Fill it in.

Why does it say "expected primary-expression before 'int'"?

A type is written inside a function call, as in Serial.println(int x);. Types go only in declarations and in a function’s first line. Pass the value alone: Serial.println(x);.

What does clang call this error?

Mostly "expected expression". With a type in a call it says "expected '(' for function-style cast or type construction", and with a type name before a dot, "does not refer to a value".

Build this for real

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