A trick with if-else

Discussion in 'C Programming' started by Piyush Agarwal, May 16, 2006.

  1. void main
    {
    int x;
    if(x)
    printf("Hello");
    else
    printf("World");
    }
    What value can be given to x such that the output is "Hello
    World"?
    Piyush Agarwal, May 16, 2006
    #1
    1. Advertising

  2. Piyush Agarwal

    Vladimir Oka Guest

    Piyush Agarwal wrote:
    > void main
    > {
    > int x;
    > if(x)
    > printf("Hello");
    > else
    > printf("World");
    > }
    > What value can be given to x such that the output is "Hello
    > World"?


    I think you misrepresented the trick question.

    With `x` an `int` it will never work.

    However, if you `#define x` appropriatelly, you can get the desired
    effect. E.g.:

    #define x (!printf("Hello "))

    will do the trick. Obviously, once you correct your code to something
    that actually resembles C:

    #include <stdio.h>

    #define x (!printf("Hello, "))

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

    printf("\n");

    return 0;
    }
    Vladimir Oka, May 16, 2006
    #2
    1. Advertising

  3. Piyush Agarwal

    Chris Dollin Guest

    Piyush Agarwal wrote:

    > void main
    > {


    `main` is a function that returns `int`, not a non-function
    of type `void` and value `bother, there was a syntax error`.

    > int x;
    > if(x)
    > printf("Hello");
    > else
    > printf("World");
    > }
    > What value can be given to x such that the output is "Hello
    > World"?


    There isn't one.

    If you didn't mean /value/, but /initialiser/, then easy-peasy.

    --
    Chris "but the if becomes irrelevant" Dollin
    "People are part of the design. It's dangerous to forget that." /Star Cops/
    Chris Dollin, May 16, 2006
    #3
  4. Piyush Agarwal said:

    > void main
    > {
    > int x;
    > if(x)
    > printf("Hello");
    > else
    > printf("World");
    > }
    > What value can be given to x such that the output is "Hello
    > World"?


    The output of the program is undefined for no fewer than three different
    reasons. So it might well say "Hello World" as written. Or, of course, it
    might not.

    Until you understand what the three reasons are, I recommend that you spend
    less time with puzzle questions and more time with a good C book.

    --
    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, May 16, 2006
    #4
  5. Piyush Agarwal wrote:
    >
    > void main
    > {
    > int x;
    > if(x)
    > printf("Hello");
    > else
    > printf("World");
    > }
    > What value can be given to x such that the output is "Hello
    > World"?


    Given that you're on Google, why not just search the c.l.c archives for
    the thread on the almost exact same question from a few weeks ago?

    Perhaps you should give us your professor's name, as I'm sure we'd all
    like to know who gives out such homework assignments.

    Of course, given your syntax error on "void main" (and numerous other
    things), your version of the "problem" is unsolvable.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, May 16, 2006
    #5
  6. Chris Dollin <> writes:

    > Piyush Agarwal wrote:
    >
    >> void main
    >> {

    >
    > `main` is a function that returns `int`, not a non-function
    > of type `void` and value `bother, there was a syntax error`.
    >
    >> int x;
    >> if(x)
    >> printf("Hello");
    >> else
    >> printf("World");
    >> }
    >> What value can be given to x such that the output is "Hello
    >> World"?

    >
    > There isn't one.
    >
    > If you didn't mean /value/, but /initialiser/, then easy-peasy.


    OK, I'll bite.... What initialiser?

    --

    John Devereux
    John Devereux, May 16, 2006
    #6
  7. Piyush Agarwal

    Suman Guest

    Vladimir Oka wrote:
    > Piyush Agarwal wrote:


    [ snip code with UB ]

    > > What value can be given to x such that the output is "Hello
    > > World"?

    >
    > I think you misrepresented the trick question.
    >
    > With `x` an `int` it will never work.


    #include <stdio.h>

    int main(void)
    {
    int x = 0;
    if ( (printf("Hello, "), x) )
    printf("Hello");
    else
    printf("World\n");
    return 0;
    }
    Suman, May 16, 2006
    #7
  8. Piyush Agarwal

    Vladimir Oka Guest

    John Devereux wrote:
    > Chris Dollin <> writes:
    >
    > > Piyush Agarwal wrote:
    > >
    > >> void main
    > >> {

    > >
    > > `main` is a function that returns `int`, not a non-function
    > > of type `void` and value `bother, there was a syntax error`.
    > >
    > >> int x;
    > >> if(x)
    > >> printf("Hello");
    > >> else
    > >> printf("World");
    > >> }
    > >> What value can be given to x such that the output is "Hello
    > >> World"?

    > >
    > > There isn't one.
    > >
    > > If you didn't mean /value/, but /initialiser/, then easy-peasy.

    >
    > OK, I'll bite.... What initialiser?


    You can do:

    int x = !printf("Hello ");

    [The "solution" I overlooked in my earlier post.]
    Vladimir Oka, May 16, 2006
    #8
  9. Piyush Agarwal

    Vladimir Oka Guest

    Suman wrote:
    > Vladimir Oka wrote:
    > > Piyush Agarwal wrote:

    >
    > [ snip code with UB ]
    >
    > > > What value can be given to x such that the output is "Hello
    > > > World"?

    > >
    > > I think you misrepresented the trick question.
    > >
    > > With `x` an `int` it will never work.

    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int x = 0;
    > if ( (printf("Hello, "), x) )
    > printf("Hello");
    > else
    > printf("World\n");
    > return 0;
    > }


    I don't think you were allowed to change the `if` statement. Have a
    look at my other post as well.
    Vladimir Oka, May 16, 2006
    #9
  10. "Vladimir Oka" <> writes:

    > John Devereux wrote:
    >> Chris Dollin <> writes:
    >>
    >> > Piyush Agarwal wrote:
    >> >
    >> >> void main
    >> >> {
    >> >
    >> > `main` is a function that returns `int`, not a non-function
    >> > of type `void` and value `bother, there was a syntax error`.
    >> >
    >> >> int x;
    >> >> if(x)
    >> >> printf("Hello");
    >> >> else
    >> >> printf("World");
    >> >> }
    >> >> What value can be given to x such that the output is "Hello
    >> >> World"?
    >> >
    >> > There isn't one.
    >> >
    >> > If you didn't mean /value/, but /initialiser/, then easy-peasy.

    >>
    >> OK, I'll bite.... What initialiser?

    >
    > You can do:
    >
    > int x = !printf("Hello ");
    >
    > [The "solution" I overlooked in my earlier post.]


    Aha! :)

    --

    John Devereux
    John Devereux, May 16, 2006
    #10
  11. Piyush Agarwal

    Suman Guest

    Vladimir Oka wrote:
    > Suman wrote:
    > > Vladimir Oka wrote:
    > > > Piyush Agarwal wrote:

    > >
    > > [ snip code with UB ]
    > >
    > > > > What value can be given to x such that the output is "Hello
    > > > > World"?
    > > >
    > > > I think you misrepresented the trick question.
    > > >
    > > > With `x` an `int` it will never work.

    > >
    > > #include <stdio.h>
    > >
    > > int main(void)
    > > {
    > > int x = 0;
    > > if ( (printf("Hello, "), x) )
    > > printf("Hello");
    > > else
    > > printf("World\n");
    > > return 0;
    > > }

    >
    > I don't think you were allowed to change the `if` statement. Have a
    > look at my other post as well.


    Show me where this constraint was mentioned ;)
    Also your _other_ post came in a minute too late, I'd
    already posted my reply before it came to being.
    Suman, May 16, 2006
    #11
  12. Piyush Agarwal

    Vladimir Oka Guest

    Suman wrote:
    > Vladimir Oka wrote:
    > > Suman wrote:
    > > > Vladimir Oka wrote:
    > > > > Piyush Agarwal wrote:
    > > >
    > > > [ snip code with UB ]
    > > >
    > > > > > What value can be given to x such that the output is "Hello
    > > > > > World"?
    > > > >
    > > > > I think you misrepresented the trick question.
    > > > >
    > > > > With `x` an `int` it will never work.
    > > >
    > > > #include <stdio.h>
    > > >
    > > > int main(void)
    > > > {
    > > > int x = 0;
    > > > if ( (printf("Hello, "), x) )
    > > > printf("Hello");
    > > > else
    > > > printf("World\n");
    > > > return 0;
    > > > }

    > >
    > > I don't think you were allowed to change the `if` statement. Have a
    > > look at my other post as well.

    >
    > Show me where this constraint was mentioned ;)
    > Also your _other_ post came in a minute too late, I'd
    > already posted my reply before it came to being.


    Well, it wasn't mentioned in so many words. I thought it was implied.
    Still, you could even do someting like:

    if ( x = !printf("Hello ") )

    or even better:

    if ( x = (printf("Hello ") != 6) )

    ;-)
    Vladimir Oka, May 16, 2006
    #12
  13. Piyush Agarwal

    Chris Dollin Guest

    Vladimir Oka wrote:

    >
    > John Devereux wrote:
    >> Chris Dollin <> writes:
    >>
    >> > Piyush Agarwal wrote:
    >> >
    >> >> void main
    >> >> {
    >> >
    >> > `main` is a function that returns `int`, not a non-function
    >> > of type `void` and value `bother, there was a syntax error`.
    >> >
    >> >> int x;
    >> >> if(x)
    >> >> printf("Hello");
    >> >> else
    >> >> printf("World");
    >> >> }
    >> >> What value can be given to x such that the output is "Hello
    >> >> World"?
    >> >
    >> > There isn't one.
    >> >
    >> > If you didn't mean /value/, but /initialiser/, then easy-peasy.

    >>
    >> OK, I'll bite.... What initialiser?

    >
    > You can do:
    >
    > int x = !printf("Hello ");
    >
    > [The "solution" I overlooked in my earlier post.]


    S'not portable - there's no final newline after the World.

    int x = (printf( "Hello, World\n" ), exit( 0 ), 17);

    will do fine.

    --
    Chris "Eyeball" Dollin
    "People are part of the design. It's dangerous to forget that." /Star Cops/
    Chris Dollin, May 16, 2006
    #13
  14. Piyush Agarwal

    Chris Dollin Guest

    Suman wrote:

    >
    > Vladimir Oka wrote:
    >> Suman wrote:
    >> > Vladimir Oka wrote:
    >> > > Piyush Agarwal wrote:
    >> >
    >> > [ snip code with UB ]
    >> >
    >> > > > What value can be given to x such that the output is "Hello
    >> > > > World"?
    >> > >
    >> > > I think you misrepresented the trick question.
    >> > >
    >> > > With `x` an `int` it will never work.
    >> >
    >> > #include <stdio.h>
    >> >
    >> > int main(void)
    >> > {
    >> > int x = 0;
    >> > if ( (printf("Hello, "), x) )
    >> > printf("Hello");
    >> > else
    >> > printf("World\n");
    >> > return 0;
    >> > }

    >>
    >> I don't think you were allowed to change the `if` statement. Have a
    >> look at my other post as well.

    >
    > Show me where this constraint was mentioned ;)


    Oh, in /that/ case:

    int main() { printf( "Hello, World\n" ); }

    will do.

    --
    Chris "Eyeball" Dollin
    "People are part of the design. It's dangerous to forget that." /Star Cops/
    Chris Dollin, May 16, 2006
    #14
  15. Piyush Agarwal

    Vladimir Oka Guest

    Chris Dollin wrote:
    > Vladimir Oka wrote:
    >
    > >
    > > John Devereux wrote:
    > >> Chris Dollin <> writes:
    > >>
    > >> > Piyush Agarwal wrote:
    > >> >
    > >> >> void main
    > >> >> {
    > >> >
    > >> > `main` is a function that returns `int`, not a non-function
    > >> > of type `void` and value `bother, there was a syntax error`.
    > >> >
    > >> >> int x;
    > >> >> if(x)
    > >> >> printf("Hello");
    > >> >> else
    > >> >> printf("World");
    > >> >> }
    > >> >> What value can be given to x such that the output is "Hello
    > >> >> World"?
    > >> >
    > >> > There isn't one.
    > >> >
    > >> > If you didn't mean /value/, but /initialiser/, then easy-peasy.
    > >>
    > >> OK, I'll bite.... What initialiser?

    > >
    > > You can do:
    > >
    > > int x = !printf("Hello ");
    > >
    > > [The "solution" I overlooked in my earlier post.]

    >
    > S'not portable - there's no final newline after the World.
    >
    > int x = (printf( "Hello, World\n" ), exit( 0 ), 17);
    >
    > will do fine.


    Yep. Using 42 instead of 17 would have been even better. ;-)

    I was however building on the code I supplied in my original reply to
    the OP (which did include the terminating '\n').
    Vladimir Oka, May 16, 2006
    #15
  16. Piyush Agarwal

    Chris Dollin Guest

    Vladimir Oka wrote:

    > Chris Dollin wrote:
    >> S'not portable - there's no final newline after the World.
    >>
    >> int x = (printf( "Hello, World\n" ), exit( 0 ), 17);
    >>
    >> will do fine.

    >
    > Yep. Using 42 instead of 17 would have been even better. ;-)


    No, it wouldn't - I didn't wish to allude to tHGttG. Instead I alluded
    to something else.

    --
    Chris "Veraly, these references are a drag." Dollin
    "People are part of the design. It's dangerous to forget that." /Star Cops/
    Chris Dollin, May 16, 2006
    #16
  17. Piyush Agarwal

    Vladimir Oka Guest

    Chris Dollin wrote:
    > Vladimir Oka wrote:
    >
    > > Chris Dollin wrote:
    > >> S'not portable - there's no final newline after the World.
    > >>
    > >> int x = (printf( "Hello, World\n" ), exit( 0 ), 17);
    > >>
    > >> will do fine.

    > >
    > > Yep. Using 42 instead of 17 would have been even better. ;-)

    >
    > No, it wouldn't - I didn't wish to allude to tHGttG. Instead I alluded
    > to something else.


    You got me there. I dont get the allusion.

    [17 *is*, more often than not, my staple "random" value, but I doubt
    you know me that well. ;-)]
    Vladimir Oka, May 16, 2006
    #17
  18. Richard Heathfield <> writes:
    > Piyush Agarwal said:
    >
    >> void main
    >> {
    >> int x;
    >> if(x)
    >> printf("Hello");
    >> else
    >> printf("World");
    >> }
    >> What value can be given to x such that the output is "Hello
    >> World"?

    >
    > The output of the program is undefined for no fewer than three different
    > reasons. So it might well say "Hello World" as written. Or, of course, it
    > might not.
    >
    > Until you understand what the three reasons are, I recommend that you spend
    > less time with puzzle questions and more time with a good C book.


    I see four reasons (one of which is unlikely to cause mere undefined
    behavior).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, May 16, 2006
    #18
  19. Piyush Agarwal wrote:
    > void main

    ^^^^ dead akready: main returns an int
    > {
    > int x;
    > if(x)

    ^^^ dead again: x is not initialized
    > printf("Hello");

    ^^^^^^ dead again; a variadic function with no declaration in
    scope
    > else
    > printf("World");

    For pre-C99 compilers (and with any decent coding standards even
    with C99), dead again without a returned value.
    > }
    > What value can be given to x such that the output is "Hello
    > World"?


    We answered this already, several times over. Please check the archives
    before posting. Although it is not relevant to your question (but is
    to your horrid code), check the FAQ before posting as well.
    Martin Ambuhl, May 16, 2006
    #19
  20. Keith Thompson said:

    > I see four reasons (one of which is unlikely to cause mere undefined
    > behavior).


    Believe it or not, the syntax error completely passed me by.

    --
    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, May 16, 2006
    #20
    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. Nuri Yilmaz

    A .Net trick everyday!

    Nuri Yilmaz, Jul 28, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    384
    Nuri Yilmaz
    Jul 28, 2004
  2. Replies:
    25
    Views:
    641
    MonkeeSage
    Oct 1, 2006
  3. kj
    Replies:
    15
    Views:
    534
    Lawrence D'Oliveiro
    May 23, 2009
  4. A
    Replies:
    8
    Views:
    773
    James Kanze
    Aug 28, 2010
  5. Gergely Kontra
    Replies:
    16
    Views:
    213
    Devin Mullins
    Sep 20, 2005
Loading...

Share This Page