Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   help with comma operator (http://www.go4expert.com/forums/help-comma-operator-t29293/)

IndiraP 16Nov2012 15:04

help with comma operator
 
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 16Nov2012 16:02

Re: help with comma operator
 
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 17Nov2012 17:53

Re: help with comma operator
 
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 17Nov2012 19:32

Re: help with comma operator
 
Thank u..:)

Rajesh M. Kanojia 12Dec2012 22:55

Re: help with comma operator
 
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 13Dec2012 08:30

Re: help with comma operator
 
Thank u.. :)


All times are GMT +5.5. The time now is 02:03.