need solution to my interview ques. in C

Discussion in 'C' started by rforravi, Aug 24, 2008.

  1. rforravi

    rforravi New Member

    Joined:
    Aug 24, 2008
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    I have been asked a question ( an interesting one ) in an interview ystrday

    I was like given the following :

    .....
    .....

    if ( xxxxxxxxxxxxx)
    printf("hello");
    else
    printf("world");


    ..
    ......



    Now, the interviewer asked me not to change anything given except the " xxxxxxxxxxx"
    He asked me to write such a condition in place of xxxx such that we get the output as :
    "helloworld"


    now.. i need to get the condition.. any help plz??
     
  2. jamsheedm

    jamsheedm New Member

    Joined:
    Aug 25, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    if(!printf(hello")) will do the trick :)
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    I don't think I would ever have guessed that.
    Once given the solution I would probably have commented something along the lines that I hope that question isn't indicative of programming standards within the company, cos if it was then I wouldn't want to work there anyway.
     
  4. jamsheedm

    jamsheedm New Member

    Joined:
    Aug 25, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    this question does have nothing to do with programming standards.tather,its purely logical question for c experts :)
     
  5. rforravi

    rforravi New Member

    Joined:
    Aug 24, 2008
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    @jamsheed m

    Dude.. thx a lot.. it was a great answer!! i was really breaking my head in the interview.. and even now.. if u dont m ind, cud u xplain this .. me trying a lot to get it into my head. it looks like i understood it but at the same time, i feel i dint get it. :crazy:

    Neways.. great answer dude!

    Thx!

    Cheerz
    R !
     
  6. jamsheedm

    jamsheedm New Member

    Joined:
    Aug 25, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Its known tht printf returns an int value and its no of characters it prints
    here printf("hello") will return 4
    and !4 is 0,so else part will be printed after hello
    so you get the reult....
    its cool na ...wat you say ravi
     
  7. rforravi

    rforravi New Member

    Joined:
    Aug 24, 2008
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    yeah. superb solution dude!!

    so, this means the statement after the 'IF' is not getting printed.. but we are writing the same statement in the condition itslf.. . So, it has to get printed by default and itz directly jumping to else part..

    Cool !!

    Cheerz
    R !!
     
  8. jamsheedm

    jamsheedm New Member

    Joined:
    Aug 25, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    a small careless error the return value is 5 not 4 :)
     

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