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?

25th Feb 2020, 1:33 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
4 ответов
+ 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.
25th Feb 2020, 1:52 PM
andriy kan
andriy kan - avatar
+ 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
25th Feb 2020, 1:40 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 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.
25th Feb 2020, 1:42 PM
MO ELomari
MO ELomari - avatar
0
Thank you all 3 of you :D
25th Feb 2020, 2:09 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar