A common idiom is the use of an operator that evaluates both operands and returns the second operand. In this way:
for(int i = 0; i != 5; ++i,++j) do_something(i,j);
But is this a comma operator?
Now, having written this, the commentator suggested that in fact it was some kind of special syntactic sugar in the for statement, and not as a comma operator. I checked this in GCC as follows:
int i=0; int a=5; int x=0; for(i; i<5; x=i++,a++){ printf("i=%da=%dx=%d\n",i,a,x); }
I expected x to take the original value of a, so it should have displayed 5,6,7 .. for x. I got it
i=0 a=5 x=0 i=1 a=6 x=0 i=2 a=7 x=1 i=3 a=8 x=2 i=4 a=9 x=3
However, if I bracketed the expression to make the parser really see the comma operator, I get this
int main(){ int i=0; int a=5; int x=0; for(i=0; i<5; x=(i++,a++)){ printf("i=%da=%dx=%d\n",i,a,x); } } i=0 a=5 x=0 i=1 a=6 x=5 i=2 a=7 x=6 i=3 a=8 x=7 i=4 a=9 x=8
At first, I thought that it showed that it does not behave like a comma operator at all, but as it turns out, this is just a priority problem - the comma operator has the lowest possible priority , therefore the expression x = i ++, a ++ is effectively parsed as ( x = i ++), a ++
Thanks for all the comments, it was an interesting learning experience and I have been using C for many years!