0
Why this code output 3/the last item?
I found a challenge like this : int i=(1,2,3); printf("%d",i); ... Why this code print 3 out?
4 Réponses
+ 4
Because comma operator is binary operator that returns the value of its second operand (the first operand is also calculated, but it result is discarded).
The comma operator is executed left to right. It has the lowest precendence (Parenthesis are used to evaluated it before assignment operator, that has higher precendence)
In the statement (1, 2, 3) the result of first comma operator is 2 (its second operand). This result (2) is used as the first operand for the second comma, and the second comma returns 3 (its second operand). So, result of that statement is 3.
+ 2
its a comma operator, in which it will use the last value found in the sequence
i=0;
while(i<=7,6,2){
printf("hi");
++i
}
this will run just twice
+ 2
the parentheses has higher precedence than assignment operator.
the expression within parentheses is evaluated from left to right but it is always the result of the last expression which gets assigned.
0
Thank you all 3 of you :D