473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1709
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lars Plessmann | last post by:
Problem: I try to store data in a objects field and read it out again. Sounds easy, yeah. But its a bit tricky here.... ;-) This is the class Customer.php with some setter and getter functions...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
12
by: why? | last post by:
I've been having problem with the following code. It's supposed to print the prime numbers between 10 and 100. But i'm not getting any output, i.e. i guess the outer 'for' loop is being traversed...
17
by: joebenjamin | last post by:
This is a problem I was trying to help a few friends figure out for fun. I am not sure how to go about this, but there is something I am missing here. Here is what we want the output to be: Need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.