help with comma operator

IndiraP's Avatar, Join Date: Nov 2012
Go4Expert Member
if i give
if(1,0)
printf("true");
else
printf("false");

then it is always evaluating to false...

but if i give i=(1,0)?1:0; printf("%d",i);

i get 1 as output ...

Actually wat does this mean???
how comma operator comes in handy in evaluating conditional expressions lyk above???
i mean its use..
IndiraP's Avatar, Join Date: Nov 2012
Go4Expert Member
sorry
i=(1,0)?1:0;
i get 0;
all i got is that if the second argument is 1 then evaluates to true..else false..
but wat is the use of it???
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
It's not a lot of use, but you could argue that
Code:
while (i++, j++, k) { ... }
is better than
Code:
i++;
j++;
while (k)
{
//...
i++;
j++;
}
on the grounds that it avoids code duplication.
IndiraP's Avatar, Join Date: Nov 2012
Go4Expert Member
Thank u..
Rajesh M. Kanojia's Avatar, Join Date: Dec 2012
Go4Expert Member
as we know that first the condition within brakect will evaluate first, but in your case u use 2 value seperater by comma operater. the comma operater evaluated from left to right.
on evaluation it find 0 as last now it keep 0 as argument of if condition.
as we no that 0 means false that's why this result comes.
IndiraP's Avatar, Join Date: Nov 2012
Go4Expert Member
Thank u..