How to Fix error: expected primary-expression before ‘)’ token (C) in C++

Error: expected primary-expression before ‘)’ token occurs in C++ at a compile-time when there is a problem with the syntax of your code, specifically that a primary expression (a variable, constant, or expression that evaluates to a value) is expected before a closing parenthesis token (‘)’).

Here are four solutions to fix this error:

  1. Ensure you are handling the parenthesis correctly.
  2. Correct syntax by including the missing operator.
  3. Adding the missing operator.
  4. Typographical errors.

Reproducing the error

#include <iostream>

int main() {
  std::cout << "Hello World!" );
  return 0;
}

Output

error: expected primary-expression before ')' token (C)

How to fix the error?

Solution 1: Ensure you are handling the parenthesis correctly

#include <iostream>

int main() {
  std::cout << "Hello World!\n";
  return 0;
}

Output

Hello World!

Missing operator

If you try to operate on a value and have forgotten to include the operator, you will get this error.

#include <iostream>

int main()
{
  int x = 2;
  std::cout << x 2; // Incorrect syntax, missing operator
  return 0;
}

Output

error: expected ';' after expression

Solution 2: Correct syntax by including the missing operator

To fix this error, add the operator you want to use, such as “+”

#include <iostream>

int main()
{
  int x = 2;
  std::cout << x + 2 << "\n";
  return 0;
}

Output

4

Missing semicolon

You will get this error if you forget to include a semicolon after a statement.

#include <iostream>

int main()
{
  int x = 2;
  std::cout << x + 2
  return 0;
}

Output

error: expected ';' after expression

Solution 3: Adding the missing operator

To fix this error, add the missing semicolon.

#include <iostream>

int main()
{
  int x = 2;
  std::cout << x + 2 << "\n";
  return 0;
}

Output

4

Solution 4: Typographical Errors:

Sometimes, typographical errors like an extra comma, a missing variable, or an accidental character can lead to this error.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.