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'?
Overview:
When writing C code, we sometimes encounter various errors. One of them is "expected primary-expression before ',' token" (missing primary-expression before comma). This error is usually detected during compilation and indicates that a valid expression is missing.
This article will explore the cause of this error and give some solutions and code examples.
Cause:
This error is usually related to syntax. It shows that somewhere in the program we put a comma where a comma is not needed. The C compiler thinks that a comma should appear between two expressions, so if we don't have an expression before the comma, this error will be raised.
Solution:
Here are a few common solutions to help you solve this problem:
For example:
void foo(int x, int y) { // 函数体 } int main() { int a = 1; foo(a, ); // 此处缺少有效的参数 return 0; }
In the above example, we forgot to provide a valid expression for the second parameter when calling function foo. This will cause the compiler to report an "expected primary-expression before ',' token" error.
To solve this problem, we need to provide valid expressions for all parameters in the function call.
For example:
int main() { int a, b; a = , b; // 此处缺少有效的表达式 return 0; }
In the above example, we forgot to provide an assignment expression for a before the comma. This will cause the compiler to report an "expected primary-expression before ',' token" error.
To solve this problem, we should provide a valid expression for the operand before the comma.
Code examples:
Here are some code examples that demonstrate how to resolve the "expected primary-expression before ',' token" error.
Example 1: Fix error in function call
void foo(int x, int y) { // 函数体 } int main() { int a = 1; int b = 2; foo(a, b); // 修复函数调用错误,为所有参数提供有效的表达式 return 0; }
Example 2: Fix error of missing operand
int main() { int a = 1; int b = 2; a = a + 1, b; // 修复缺少操作数的错误,给逗号之前的操作数提供有效的表达式 return 0; }
Summary:
In C programming, we may You will encounter various grammatical errors. Understanding and solving these errors is an important step in becoming a better programmer. This article describes how to resolve the C syntax error "expected primary-expression before ',' token" and provides some code examples for function calls and operands.
Hope this article will help you solve this problem. Happy coding!
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!