help with comma operator

Go4Expert Member
16Nov2012,15:04   #1
IndiraP's Avatar
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..
Go4Expert Member
16Nov2012,16:02   #2
IndiraP's Avatar
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???
Mentor
17Nov2012,17:53   #3
xpi0t0s's Avatar
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.
Go4Expert Member
17Nov2012,19:32   #4
IndiraP's Avatar
Thank u..
Go4Expert Member
12Dec2012,22:55   #5
Rajesh M. Kanojia's Avatar
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.
Go4Expert Member
13Dec2012,08:30   #6
IndiraP's Avatar
Thank u..