I was reading this question about sequence points and I saw this line:
i = (i, ++i, 1) + 1; // well defined (AFAIK)
I was wondering how is the following syntax called and what are its effects?
i = (a1, a2, ...,an);
I was reading this question about sequence points and I saw this line:
i = (i, ++i, 1) + 1; // well defined (AFAIK)
I was wondering how is the following syntax called and what are its effects?
i = (a1, a2, ...,an);
This is the comma operator for int
operands, together with grouping through parentheses, which is always allowed. First,
(i, ++i, 1)
evaluates i
, then ++i
, then 1
and returns the result of the last expression (which is 1
). Then
(i, ++i, 1) + 1
is the same as
1 + 1;
which results in 2
, so i
is set to 2
here. Note that without the parentheses, the result would in most cases not be the same, as the comma operator has the lowest possible precedence (thanks to @dbush for helping me out here in the comments).