a tricky if else(maybe not tricky but impossible)

Discussion in 'C Programming' started by nirkheys@gmail.com, Apr 25, 2006.

  1. Guest

    Hi all,

    I have been asked if the following is possible.
    i googled for it,thought a lot about it and decided that it is
    not possible,but i am posting it here in a hope of getting this
    problem solved.

    ---------------------
    if(x)
    {
    printf("Hello");
    }
    else
    {
    printf("world");
    }Is there any value of x for which output would be Hello world?

    -------------------------
    Thanking you all in advance

    Just Saurabh
     
    , Apr 25, 2006
    #1
    1. Advertising

  2. said:

    > Hi all,
    >
    > I have been asked if the following is possible.
    > i googled for it,thought a lot about it and decided that it is
    > not possible,but i am posting it here in a hope of getting this
    > problem solved.
    >
    > ---------------------
    > if(x)
    > {
    > printf("Hello");
    > }
    > else
    > {
    > printf("world");
    > }Is there any value of x for which output would be Hello world?


    Yes.

    #define x printf("Hello ") == 42


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Apr 25, 2006
    #2
    1. Advertising

  3. Vladimir Oka Guest

    wrote:
    > Hi all,
    >
    > I have been asked if the following is possible.
    > i googled for it,thought a lot about it and decided that it is
    > not possible,but i am posting it here in a hope of getting this
    > problem solved.
    >
    > ---------------------
    > if(x)
    > {
    > printf("Hello");
    > }
    > else
    > {
    > printf("world");
    > }Is there any value of x for which output would be Hello world?


    No.

    OTH, if somewhere you don't terminate output with '\n' or flush it, you
    may see nothing at all.
     
    Vladimir Oka, Apr 25, 2006
    #3
  4. arun Guest

    If x can be anything ??
    Give a printf statement which prints "hello" in the place of x.
    And negate the whole thing. Put it as --> if ( !x ).
    The printf will return 5 and the negation will make the condition fail
    and the printf in the else part will work. It will print "hello world".

    Regards,
    arun..
     
    arun, Apr 25, 2006
    #4
  5. Zero Guest

    schrieb:

    > Hi all,
    >
    > I have been asked if the following is possible.
    > i googled for it,thought a lot about it and decided that it is
    > not possible,but i am posting it here in a hope of getting this
    > problem solved.
    >
    > ---------------------
    > if(x)
    > {
    > printf("Hello");
    > }
    > else
    > {
    > printf("world");
    > }Is there any value of x for which output would be Hello world?
    >
    > -------------------------
    > Thanking you all in advance
    >
    > Just Saurabh


    Perhaps you don't understand if/else...
    it's just either .. or and not both :)
     
    Zero, Apr 25, 2006
    #5
  6. Guest

    Re: Re:a tricky if else(maybe not tricky but impossible)

    arun wrote:
    > If x can be anything ??
    > Give a printf statement which prints "hello" in the place of x.
    > And negate the whole thing. Put it as --> if ( !x ).
    > The printf will return 5 and the negation will make the condition fail
    > and the printf in the else part will work. It will print "hello world".
    >
    > Regards,
    > arun..


    Thanks a lot Arun.
    You solved my problem.


    Just Saurabh
     
    , Apr 25, 2006
    #6
  7. Guest

    Re: Re:a tricky if else(maybe not tricky but impossible)

    arun wrote:
    > If x can be anything ??
    > Give a printf statement which prints "hello" in the place of x.
    > And negate the whole thing. Put it as --> if ( !x ).
    > The printf will return 5 and the negation will make the condition fail
    > and the printf in the else part will work. It will print "hello world".
    >
    > Regards,
    > arun..


    Thanks a lot Arun.
    You solved my problem.


    Just Saurabh
     
    , Apr 25, 2006
    #7
  8. wrote:
    > Hi all,
    >
    > I have been asked if the following is possible.
    > i googled for it,thought a lot about it and decided that it is
    > not possible,but i am posting it here in a hope of getting this
    > problem solved.
    >
    > ---------------------
    > if(x)
    > {
    > printf("Hello");
    > }
    > else
    > {
    > printf("world");
    > }Is there any value of x for which output would be Hello world?



    #include <stdio.h>
    #define x (printf("Hello "),fflush(stdout),0)

    int main(void)
    {
    printf("[output:]\n");
    if (x)
    printf("Hello");
    else
    printf("world");
    putchar('\n');
    return 0;
    }

    [output:]
    Hello world
     
    Martin Ambuhl, Apr 25, 2006
    #8
  9. Ben C Guest

    On 2006-04-25, <> wrote:
    > Hi all,
    >
    > I have been asked if the following is possible.
    > i googled for it,thought a lot about it and decided that it is
    > not possible,but i am posting it here in a hope of getting this
    > problem solved.
    >
    > ---------------------
    > if(x)
    > {
    > printf("Hello");
    > }
    > else
    > {
    > printf("world");
    > }Is there any value of x for which output would be Hello world?


    Here's one:

    #include <stdio.h>

    int y = 1;
    #define x (y-- ? main() : 1)

    int main(void)
    {
    if (x)
    printf("Hello\n");
    else
    printf("World\n");

    return 0;
    }
     
    Ben C, Apr 25, 2006
    #9
  10. CBFalconer Guest

    Richard Heathfield wrote:
    > said:
    >>
    >> I have been asked if the following is possible.
    >> i googled for it,thought a lot about it and decided that it is
    >> not possible,but i am posting it here in a hope of getting this
    >> problem solved.
    >>
    >> ---------------------
    >> if(x)
    >> {
    >> printf("Hello");
    >> }
    >> else
    >> {
    >> printf("world");
    >> }Is there any value of x for which output would be Hello world?

    >
    > Yes.
    >
    > #define x printf("Hello ") == 42


    A nice devious mind at work :)

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, Apr 25, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    5
    Views:
    622
  2. Ville Vainio
    Replies:
    11
    Views:
    617
    Hamish Lawson
    Aug 10, 2004
  3. shapper
    Replies:
    1
    Views:
    369
    clintonG
    Nov 29, 2006
  4. Joshua Moore
    Replies:
    8
    Views:
    340
    Old Wolf
    Apr 18, 2007
  5. Replies:
    5
    Views:
    263
    Michele Dondi
    Jun 30, 2006
Loading...

Share This Page