i = (a, b); // stores b into i
// ... a=1, b=2, c=3, i=2
i = a, b; // stores a into i. Equivalent to (i = a), b;
// ... a=1, b=2, c=3, i=1
Check property of comma operator.
"In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type)." - wikipedia
C uses the comma (,) in two distinct ways: as an element separator in compound constructs such as array literals or lists of declarations, and as a binary operator. It does not have list-based assignment such as some higher-level languages do.
As an operator, the comma evaluates its left-hand operand first, then evaluates its right-hand operand. The value of the overall expression is the second result. This is in some ways complementary to the && and || operators, both of which evaluate their left-hand operands first, but each of which evaluates its right-hand operand only conditionally, depending on the left-hand result.
The other key to understanding your observation is that the equals sign (=) is also an operator. It evaluates both operands, in unspecified order, and its result is the same as the right-hand operand. Modifying the value of the left-hand operand is a side effect.
The assignment operator has very low precedence, but the comma operator has the lowest precedence of all. Therefore, if you do not use parentheses to alter evaluation order, this ...
a = 4,3;
... is equivalent to ...
(a = 4), 3;
. It evaluates the assignment first, yielding the value 4 with the side effect of assigning that value to variable a. Then it discards that value and evaluates 3, yielding the value 3 as the overall result. Since the whole thing is not part of any larger expression, the result is discarded.
On the other hand, you can override precedence with suitable use of parentheses, as in this:
a = (4, 3);
. In that case, the comma operator is evaluated first, yielding 3 as its result, and that is the right-hand operand of the assignment operator. The assignment expression yields result 3
In C/C++, comma(,) operator is evaluated left-to-right, and the value of the left expression is discarded. The type and value of the result are the type and value of the right operand. All side effects from the evaluation of the left-operand are completed before beginning the evaluation of the right operand. Taken from KR book A.7.18
Also, comma operator has the least precedence and order of evaluation.
As = operator has higher precedence than , operator. In following statement
a = 4,3;
assignment happens first resulting in a = 4.
and also as we all know, () overrides the precedence, so we have a = 3 in case of
int a = (4,3);
Now, We all understood the theory of , and =, Let us concentrate when to use ,. Comma operator should be used sparingly, The most suitable uses are for constructs strongly related to each other. for instance in for loop
for (i = 0, j = strlen(s)-1; i < j; i++, j--)
Wherever possible, I will use () in conjunction with ,
Asked in February 2016Viewed 1,859 timesVoted 5Answered 3 times