Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > a tricky if else(maybe not tricky but impossible)

Reply
Thread Tools

a tricky if else(maybe not tricky but impossible)

 
 
nirkheys@gmail.com
Guest
Posts: n/a
 
      04-25-2006
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

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      04-25-2006
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)
 
Reply With Quote
 
 
 
 
Vladimir Oka
Guest
Posts: n/a
 
      04-25-2006

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.

 
Reply With Quote
 
arun
Guest
Posts: n/a
 
      04-25-2006
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..

 
Reply With Quote
 
Zero
Guest
Posts: n/a
 
      04-25-2006

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

 
Reply With Quote
 
nirkheys@gmail.com
Guest
Posts: n/a
 
      04-25-2006

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

 
Reply With Quote
 
nirkheys@gmail.com
Guest
Posts: n/a
 
      04-25-2006

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

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-25-2006
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
 
Reply With Quote
 
Ben C
Guest
Posts: n/a
 
      04-25-2006
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;
}
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      04-25-2006
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/>

 
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
Re: "But..but...but...I can see Russia from my porch!" mrLookout@sewage.com Computer Support 0 04-15-2010 12:02 AM
Another Tricky Problem I am Messing With (Not Homework) joebenjamin C Programming 31 10-11-2007 05:32 PM
Nested array filling well.. but tricky search back Josselin Ruby 0 09-24-2007 10:58 AM
Want to call jscript "confirm" box from VB.NET, but NOT from a but =?Utf-8?B?SlQ=?= ASP .Net 0 03-21-2006 12:14 AM
Connect to 802.11b but not 802.11g... but used to be able to...? jbraly@gmail.com Wireless Networking 1 03-13-2006 11:05 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