1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

help with comma operator

Discussion in 'C' started by IndiraP, Nov 16, 2012.

  1. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    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..
     
  2. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    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???
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  4. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    Thank u..:)
     
  5. Rajesh M. Kanojia

    Rajesh M. Kanojia New Member

    Joined:
    Dec 9, 2012
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    INDIA
    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.
     
  6. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    Thank u.. :)
     

Share This Page