The comma operator has left-to-right associativity. Two expressions separated by a comma are evaluated left to right. The left operand is always evaluated, and all side effects are completed before the right operand is evaluated.
- The comma operator is mainly useful for obfuscation.
- The comma operator allows grouping expression where one is expected.
- Commas can be used as separators in some contexts, such as function argument lists.
Example :
#include <iostream>
using namespace std;
int main ()
{
int i = 10, b = 20, c= 30;
// here, we are using comma as separator
i = b, c;
cout<<i<<"\n";
// bracket makes its one expression.
// all expression in brackets will evaluate but
// i will be assigned with right expression (left-to-right associativity)
i = (b, c);
cout<<i<<"\n";
return 0;
}
Output :
20 30
0 Comments