Home  >  Article  >  Backend Development  >  C++ error: comma is not allowed in assignment statement, how to deal with it?

C++ error: comma is not allowed in assignment statement, how to deal with it?

WBOY
WBOYOriginal
2023-08-22 08:18:231510browse

When programming in C, we often encounter various errors. One of the common errors is "commas are not allowed in assignment statements." This kind of error will cause the program to fail to compile and affect the normal operation of the program, so it must be dealt with in time.

The reason for this error is that in C, commas have two functions: one is a separator and the other is an operator. If you use a comma as an operator in an assignment statement, an error message "Comma is not allowed in assignment statements" will appear.

So, how to deal with this error?

First of all, we need to clarify the two functions of commas in C. As a delimiter, the comma is used to separate multiple statements or declarations; as an operator, the comma is used to execute multiple expressions and return the value of the last expression.

Secondly, you need to check whether the comma is used as an operator in the code. If it exists, it needs to be modified. Multiple expressions can be assigned to different variables, avoiding the use of commas in assignment statements.

For example, the following code will report an error:

int a = 1, b = 2, c = 3;
a = b, c;

at In this code, the comma operator is used in the assignment statement, causing an error. The code can be modified as:

int a = 1, b = 2, c = 3;
a = b;
a = c;

This modified code No longer using comma as operator, compiles successfully.

In addition, you can also consider using control structures such as if statements and for loops to avoid using the comma operator in assignment statements.

In general, if you encounter the error "comma is not allowed in assignment statements", you need to check whether the comma is used as an operator in the code and modify it in time. Note that in C programming, proper use of commas as separators and operators can avoid this error.

The above is the detailed content of C++ error: comma is not allowed in assignment statement, how to deal with it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn