Home >Backend Development >C++ >How to solve C++ syntax error: 'expected primary-expression before ';' token'?
How to solve C syntax error: 'expected primary-expression before ';' token'
When learning and writing C code, we often encounter various syntaxes mistake. One of the common errors is 'expected primary-expression before ';' token'. This error often occurs when a semicolon is used to end a statement, but the structure of the statement is incorrect. This article details the cause of this error and provides a solution.
There are usually two reasons for this error: the variable is not defined correctly or the wrong syntax structure is used. Below we will explain these two situations respectively and provide corresponding solutions.
Situation 1: Incorrectly defined variables
When we reference an incorrectly defined variable in C code, the compiler will report an error 'expected primary-expression before ';' token '. This usually happens in the following situations:
x = 5;
In the above code, the type of variable x is not defined, and the compiler will report an error.
Solution: Declare the type of the variable correctly. For example:
int x = 5;
Solution: Check whether the variable names in the code are spelled correctly and correct the errors.
int num = 10; cout << nums << endl; // 正确的变量名是num,不是nums
void func() { int x = 5; } int main() { cout << x << endl; // x在函数func的作用域内,无法在main函数中引用 return 0; }
Solution: Place the variable declaration in the appropriate scope.
Case 2: Using the wrong grammatical structure
When we use the wrong grammatical structure in the C code, the compiler will report the error 'expected primary-expression before ';' token '. This usually occurs in the following situations:
Solution: Check whether the expressions in the code are correct and correct the errors.
Solution: Check whether the member access operation in the code is correct and correct the error.
Here is a simple code example that demonstrates how to solve the C syntax error 'expected primary-expression before ';' token':
#include <iostream> using namespace std; int main() { int num = 10; cout << num << endl; return 0; }
In the above code, we have declared the variables correctly The type of num and initialized before using the variable. Therefore, the compiler will not issue an error.
Summary:
When writing C code, we often encounter various syntax errors. When the 'expected primary-expression before ';' token' error occurs, we need to carefully check the code to find out and solve the cause of the error. This article provides two common error conditions and corresponding solutions, hoping to help readers solve this error. At the same time, it is recommended to maintain good coding style and specifications when coding, which can effectively reduce the occurrence of grammatical errors.
The above is the detailed content of How to solve C++ syntax error: 'expected primary-expression before ';' token'?. For more information, please follow other related articles on the PHP Chinese website!