Examples Using the Comma Operator ( , )

The table below gives some examples of the uses of the comma operator:

Statement Effects
for (i=0; i<2; ++i, f() );
A for statement in which i is incremented and f() is called at each iteration.
if ( f(), ++i, i>1 )
   { /* ... */ }
An if statement in which function f() is called, variable i is incremented, and variable i is tested against a value. The first two expressions within this comma expression are evaluated before the expression i>1. Regardless of the results of the first two expressions, the third is evaluated and its result determines whether the if statement is processed.
func( ( ++a, f(a) ) );
A function call to func() in which a is incremented, the resulting value is passed to a function f(), and the return value of f() is passed to func(). The function func() is passed only a single argument, because the comma expression is enclosed in parentheses within the function argument list.


Comma Operator ( , )