Home >Backend Development >C#.Net Tutorial >What is the usage of comma operator in C language?

What is the usage of comma operator in C language?

烟雨青岚
烟雨青岚Original
2020-07-01 16:11:5210193browse

The comma operator in C language is used as a sequence point. The combination order is from left to right and is evaluated sequentially. The values ​​of expressions separated by commas are settled separately, but the value of the entire expression is The value of the last expression. The comma operator (,) is the operator with the lowest precedence among C language operators; it can be used as a separator in a statement.

What is the usage of comma operator in C language?

The comma operator means that in C language, multiple expressions can be separated by commas, where the values ​​of expressions separated by commas are resolves, but the value of the entire expression is the value of the last expression.

When used in sequence, the combination order is from left to right, and is used for sequential evaluation. After completion, the value of the entire expression is the value of the last expression.

Example:

int a1,a2,b=2,c=7,d=5; // 第1行
a1=(++b,c--,d+3); // 第2行
a2=++b,c--,d+3; //第3行

For the code that assigns a1 a value, there are three expressions, separated by commas, so the final value should be the last The value of an expression, that is, the value of (d 3), is 8, so the value of a1 is 8.

There are also three expressions for the code that assigns a value to a2. The three expressions at this time are a2= b, c--, d 3, (this is because the assignment operator takes precedence over the comma operator level) Although the value of the final expression is also 8, b=4 (when the second line of code is completed, b=3, that is, when the third line of code is run, the value of b is 4), so a2=4 .

Recommended tutorial: "C Language"

The above is the detailed content of What is the usage of comma operator in C language?. 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