doubt

Discussion in 'C' started by jainprateek2000, Aug 24, 2009.

  1. jainprateek2000

    jainprateek2000 New Member

    Joined:
    Aug 24, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include<iostream.h>
    void main()
    {
    float num=110;
    (num!=(num=--num))?cout<<"OK":cout<<"NOT OK";
    }
    Give the output with detailed explanation
     
    Last edited by a moderator: Aug 25, 2009
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Run it and see what is the ouput and if you cannot justify it ask us why the output is OK/NOT OK
     
  3. c_user

    c_user New Member

    Joined:
    Aug 23, 2009
    Messages:
    86
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Php dev
    Location:
    Bhubaneswar
    as per my logic the answer is NOT OK...
    because see the line
    (num!=(num=--num))?cout<<"OK":cout<<"NOT OK";
    is a ternary operator and
    the words num=--num means that the num value is decreased by one and it is assigned to num
    so now the new value of num is 109 and
    the latter set of staments compares
    109 with 109 [ since we know that -- operator decreases it by one so the answer is "not ok"

    if u need more clear explanation i m ready to explain..
    hav a gud day..
     
  4. mayjune

    mayjune New Member

    Joined:
    Jun 14, 2009
    Messages:
    814
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Pune,Delhi
    I agree with shabbir...
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    The output is undefined because you modify the same variable twice in the same statement.
    So the output will depend on how the compiler writer felt at the time, and may differ from compiler to compiler and version to version of the same compiler.
     
  6. mayjune

    mayjune New Member

    Joined:
    Jun 14, 2009
    Messages:
    814
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Pune,Delhi
    I think xp is right about this.. even i read something like this long back...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice