Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > A trick with if-else

Reply
Thread Tools

A trick with if-else

 
 
Suman
Guest
Posts: n/a
 
      05-16-2006

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.

 
Reply With Quote
 
 
 
 
Vladimir Oka
Guest
Posts: n/a
 
      05-16-2006

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) )



 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      05-16-2006
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/
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      05-16-2006
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/
 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      05-16-2006

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').

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      05-16-2006
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/
 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      05-16-2006

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. ]

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-16-2006
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) kst- <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.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      05-16-2006
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.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-16-2006
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
profile trick, really anoying Splibbilla Firefox 2 06-29-2005 08:39 PM
so... what's the trick in getting wireless working on Server 2003?! pif Wireless Networking 1 10-13-2004 04:21 PM
A .Net trick everday! Nuri YILMAZ ASP .Net 0 08-09-2004 07:58 AM
A .Net trick everyday! Nuri Yilmaz ASP .Net 0 07-28-2004 01:46 PM
Is this trick with reset acceptable? valentin tihomirov VHDL 6 04-14-2004 05:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57