C++ Comma Operator



The purpose of comma operator is to string together several expressions. The value of a comma-separated list of expressions is the value of the right-most expression. Essentially, the comma's effect is to cause a sequence of operations to be performed.

How Comma Operator Works?

The values of the other expressions will be discarded. This means that the expression on the right side will become the value of the entire comma-separated expression. For example −

 var = (count = 19, incr = 10, count+1); 

Here first assigns count the value 19, assigns incr the value 10, then adds 1 to count, and finally, assigns var the value of the rightmost expression, count+1, which is 20. The parentheses are necessary because the comma operator has a lower precedence than the assignment operator.

Example of Comma Operator

To see the effects of the comma operator, try running the following program −

 #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 produces the following result −

 1010 

Here is the procedure how the value of i gets calculated: j starts with the value 10. j is then incremented to 11. Next, j is added to 100. Finally, j (still containing 11) is added to 999, which yields the result 1010.

Use Cases of Comma Operator

1. Using Comma Operator in Variable Initialization

In the following example, we use the comma operator to initialize multiple variables in a single statement.

 #include <iostream> using namespace std; int main() { int a, b, c; // Using comma operator for initialization a = (b = 10, c = 20); cout << "Value of a: " << a << endl; cout << "Value of b: " << b << endl; cout << "Value of c: " << c << endl; return 0; } 

When executed, this program outputs:

 Value of a: 20 Value of b: 10 Value of c: 20 

2. Using Comma Operator in Loops

The comma operator can be used in loops to update multiple variables.

 #include <iostream> using namespace std; int main() { // Using comma operator in a for loop for (int i = 0, j = 5; i < 5; i++, j--) { cout << "i: " << i << ", j: " << j << endl; } return 0; } 

When executed, this program outputs:

 i: 0, j: 5 i: 1, j: 4 i: 2, j: 3 i: 3, j: 2 i: 4, j: 1 

3. Using Comma Operator in Function Arguments

The comma operator can be used in function calls where multiple expressions are evaluated, but only the last value is passed as an argument.

 #include <iostream> using namespace std; // Function that takes an integer parameter void display(int x) { cout << "Received value: " << x << endl; } int main() { int a = 5, b = 10; // Using comma operator in function arguments display((a += 5, b += 20)); return 0; } 

When executed, this program outputs:

 Received value: 30 

4. Using Comma Operator in if Statements

The comma operator can be useful in conditional statements when multiple expressions need to be checked.

 #include <iostream> using namespace std; int main() { int x = 10, y = 20; // Using comma operator in if condition if (x += 5, y -= 10, x > y) { cout << "x is greater than y" << endl; } else { cout << "y is greater than or equal to x" << endl; } return 0; } 

When executed, this program outputs:

 x is greater than y 
Advertisements
close