English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The comma operator is used to combine several expressions together.
The value of the entire comma expression is the value of the last expression in the series.
In essence, the comma operator is used to execute a series of operations in sequence.
The value of the expression on the rightmost side will be used as the value of the entire comma expression, and the values of the other expressions will be discarded. For example:
var = (count=19, incr=10, count+1);
Here, first assign the value to count 19, and assign the value of incr to 10, then add the value of count 1, finally, assign the value of the expression on the right side to+1 The result of the calculation 20 Assign to var. The parentheses in the above expression are necessary because the precedence of the comma operator is lower than that of the assignment operator.
Try running the following example to understand the usage of the comma operator.
#include <iostream> using namespace std; int main() { int i, j; j = 10; i = (j++, j+100, 999+j); cout << i; return 0; }
When the above code is compiled and executed, it will produce the following result:
1010
In the above program, the initial value of j is 10, then increment to 11, then add 10, then add j again 999, resulting in 1010.