I have:
for (i=0,i2=0;i<10;i++,i2++)
{
.. some code
}
Is "," (comma) oke here?
Yes.
I am converting below code to for -loop:
i=0;
i2=0;
while (i<10)
{
... some code
i++;
i2++;
}
Which is executed first in for (i=0,i2=0;i<10;i++,i2++)? i2=0 or i=0?
The expression i=0 is executed first, though in this particular case it
doesn't matter.
Any caveats? I could not find explanation to comma usage (maybe not not looking
hard enough).
The expression E1,E2, where E1 is any expression, and E2 is any
expression other than another comma expression, causes expression E1 to
be evaluated. The result of that evaluation is discarded, so it only
make sense to do this if E1 is an expression with side effects, such as
a=3, b++, or printf("error:%g", d). After all side effects of E1 are
complete, expression E2 is evaluated. The value of expression E2 is also
the value of the comma expression as a whole. Thus "a = (b++,c)" is
equivalent to "b++; a=c;". It only makes sense to use a comma expression
when such a re-write is not possible, as in a for() statement such as
the one you ask about above.
A key point to understand is that commas are NOT always part of a comma
expression. The parameters of a function declaration and the arguments
of a function call are separated by commas, and the same is true for
function-like macros. Commas separate the initializers for the elements
of an array or members of a structure. A single declaration can declare
multiple declarators, separated by commas, as in "int a,b;". Commas are
used to separate the members of a union or enumeration. Commas are also
used in _Generic() expressions and _Static_assert() declarations, which
are new features of C2011, syntactically similar to function calls.
If you wish to use a comma expression in one of those contexts, you'll
have to parenthesize it, to prevent misinterpretation of the comma.
printf("hello %s", "world!\n") does something different from
printf( ("hello %s", "world!\n") ).