This works because you use the “comma operator”, which evaluates the subexpressions on the left and right and has the value from the right expression.
So, in (1,2,3,4,5) , 1 is evaluated and the result is discarded, then 2,3,4,5 ... in which (due to the next comma) 2 is evaluated and the result is discarded, then 3,4,5 ... in which 3 is evaluated and discarded, then 4,5 ... in which 4 is evaluated and discarded, then 5 , which becomes the result of the expression.
As for when it is useful, mainly as a shortcut, when you need to evaluate a few (sub) expressions for their side effects, but they are not interested in their meanings (except, perhaps, the latter). Sometimes this is convenient in for loop expressions, for example, when adding two variables:
for (i=0,j=1; j < len; i++,j++) {
.. where it appears both in the initialization expression and in the loop expression.
Dmitri
source share