How to solve Expected Primary Expression Before

 While writing a program, a programmer is sure to face some obscurities. Sometimes it is difficult to find what causes an error in the piece of code that you have written. The error you face can be a logical error, compiling error, or a simple syntax error. In C++, you might have encountered the error “expected primary expression” which usually occurs due to a syntax error In this article, we are going to look over the details of the error with the help of examples.

What is the error and why does it occur? #

The ‘expected primary expression‘ is one of the most common syntax errors which occur due to a missing keyword or an irrelevant keyword. The error prompt displays the location where the keyword is missing. 
These mistakes are encountered by beginners and people who write their programs in a hurry. The syntax of the error prompt looks something like this:


/tmp/35HQ008IAw.cpp:11:48: error: expected primary-expression before 'words'
int wordLength  = wordLengthFunction(string words);

The keyword ‘words’ in the above example denotes where the keyword is missing or added.

Reasons for the Error #

There are specific reasons why the programmer can face this error and some of the reasons can be:

Specifying Data Type Along With Function Calling

One of the most common mistakes that novice coders make is passing the data type along with the function-calling argument.For Example, we will be creating a function to add 2 numbers and pass two integers in the function. When calling the function, we will specify the data type again which will trigger the error. The code for the above example will be:

        using namespace std;
        int add(int num_one, int num_two)
        int result;
        result = num_one + num_two;
        return result;
        int main()
        int answer = add(int 100, int 230);
        return 0;

The above code contains the data type declaration along with the function-calling argument which will generate the following output


            /tmp/CVK1ULcyYx.cpp:12:18: error: expected primary-expression before 'int'
              12 | int answer = add(int 100, int 230);
              |                                         ^~~
             /tmp/CVK1ULcyYx.cpp:12:27: error: expected primary-expression before 'int'
               12 | int answer = add(int 100, int 230);
                   |                           ^~~

The output for the code is the specified error and the location where we have declared our data types again.

Wrong Argument Type

A lot of times if you pass the wrong argument in a particular function it will produce the same ‘primary expression’ error. For Example, if you declare a string in a function and then pass an integer in the argument.

        int main()
        int answer = add("Hello", "How are You" );
        return 0;

When the above code is executed, we will face the same error which will tell us that we passed a string to a function as we cannot pass a string to the function.

Incomplete Curly Braces

If the number of curly braces is not even or are extra than the requirement then the developer is likely to face this error. An example of incomplete curly braces would be:

        int add(int num_1, int num_2 )
        int result;
        result = num_1 + num_2;
        return result;

In the above code, you might notice that there is no closing brace which will result in the stated error.

Note: The error can also be invoked if the parenthesis in an if statement is empty

Solutions to the error#

Now that we know about the reasons for the occurrence of the error we can find solutions to fix the error or measures to prevent it.  The solutions for correcting the error are given below:

Removing the data type while calling function

You have to avoid writing the data type in the parenthesis while calling a function. This is a way to solve the error. The proper way to pass arguments to a function is:

    int main() {
    int answer = add( 100, 230);
    return 0;

While passing the arguments in a function make sure that you double-check for any missing arguments through the function.

Using Correct Data Type

You must ensure that the data type of the argument matches the data type which is specified in the given parameter which will ensure that the error does not occur. An example would be passing integers in the function “add” instead of strings.

Even Number of Curly Braces

The program should be carefully observed and should have even number of opening and closing brackets. If they are not, make sure to look for the brace which is incomplete. The below example will demonstrate this point.

    Int main(){
    Int a,b;
    Int c = a+b;

The above output is having an opening curly brace after ‘main’ and a closing curly brace at the end which will reduce the likeliness of occurrence of the stated error.

The Conclusion

In this article we dived deep into “expected primary expression before” which is merely a syntax error and can be solved with the help of some observation and attentiveness while writing the program. The programmer should remember the key points like never pass parameters along with arguments when calling a function and ensuring equal number of opening and closing curly braces so as to avoid facing this error.

Next Post Previous Post
No Comment
Add Comment
comment url