470,841 Members | 921 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,841 developers. It's quick & easy.

a tricky if else(maybe not tricky but impossible)

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 '06 #1
9 1618
ni******@gmail.com 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)
Apr 25 '06 #2

ni******@gmail.com 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.

Apr 25 '06 #3
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..

Apr 25 '06 #4

ni******@gmail.com 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 :-)

Apr 25 '06 #5

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 '06 #6

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 '06 #7
ni******@gmail.com 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
Apr 25 '06 #8
On 2006-04-25, ni******@gmail.com <ni******@gmail.com> 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;
}
Apr 25 '06 #9
Richard Heathfield wrote:
ni******@gmail.com 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/>

Apr 25 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.