Interesting C Question

Discussion in 'C' started by ndakota, Sep 28, 2007.

  1. ndakota

    ndakota New Member

    Joined:
    Aug 9, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    void main()
    {
    		 if(CONDITION)
    			   printf("Hello ");
    		 else
    			   printf("World");
    }
    
    Replace CONDITION with such a condition that both the printf statements get executed.
    You can't have a loop or other things in main()
    According to my knowledge it's not possible.
    But if it is possible, please let me know.
    Thanks
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    printf("World"); would be nice option
     
  3. ndakota

    ndakota New Member

    Joined:
    Aug 9, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    A single if-else with a condition cannot take both branches. It's not an interesting question, it's a silly question.
     
  5. An007kit

    An007kit New Member

    Joined:
    Oct 1, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Am a student
    Location:
    Live in chandigarh, india
    The answer is....

    condition --->if(!printf("hello"))
     
  6. An007kit

    An007kit New Member

    Joined:
    Oct 1, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Am a student
    Location:
    Live in chandigarh, india
    Well dawei......that is not a silly question......actually that's a tricky question...... ;)
     
  7. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Nothing tricky about it. It's silly. Your amazing solution only has one printf statement so it can't possibly
    That isn't a trick; that's being unable to read and comprehend.
     
  8. An007kit

    An007kit New Member

    Joined:
    Oct 1, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Am a student
    Location:
    Live in chandigarh, india
    DaWei....there are still two printf statements that get get executed.....one that prints hello and other that prints world.......so i think there is no problem in reading neither in comprehension..... :)
     

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