The Comma Operator


         expression, assignment_expression


The comma operator groups left-to-right. A pair of expressions that are separated by a comma are evaluated left-to-right, and the result of left expression is discarded. The type and value of the return are determined by the right expression. The result is an lvalue if the right expression is an lvalue. Because of the left-to-right evaluation, any side effects of the left expression take place prior to the evaluation of the right expression.

The comma has a special meaning in certain situations (e.g. when supplying a parameter list during a function call). When the context is such that a special meaning is ascribed to the comma, the comma operator can be used only if it and its operands are enclosed within parentheses. Consider the following example.

int add(int i,int j) {return i+j;} // define function add
int i=0,j=1;                       // declare two variables
int k = add(i,(i++,j++,j));        // comma operator used to increment i and j
                                   // (i++,j++,j) must be enclosed within parentheses

The expressions i++ and j++ are evaluated prior to the value of j (2) being passed to add. The value of i when passed as a parameter to add is implementation dependent (but is either 0 or 1 - see note on the order of parameter evaluation in the topic of function call).