LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-20-2004, 01:50 AM   #1
sansk
LQ Newbie
 
Registered: Mar 2004
Posts: 6

Rep: Reputation: 0
Simple C Puzzle


main()
{
if(condition)
printf("Hello ");
else
printf("world");
}



what should be the condition so that output is
Hello world


Thanks in advance
sansk
 
Old 05-20-2004, 07:40 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Not possible. Depending on the condition either the statement after the "if" or the statement after the "else" will be executed.
 
Old 05-20-2004, 10:05 AM   #3
bluie
LQ Newbie
 
Registered: May 2004
Location: Europe
Distribution: Mutable
Posts: 10

Rep: Reputation: 0
Not possible? But the following code compiles and gives the requested output.
Code:
#include <stdio.h>

int
foo()
{
  printf("Hello ");
  return 0;
}

#define condition foo()

int
main()
{
if(condition)
printf("Hello ");
else
printf("world");
}


// Ok, ok, ok.  Just kidding ;)
Bye, bluie
 
Old 05-20-2004, 11:16 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
// Ok, ok, ok. Just kidding
Since it was called a "puzzle" by the original poster, I suppose that's the correct answer!
I didn't think of that. Nice.
 
Old 05-20-2004, 11:19 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Shorter solution:
Code:
main()
{
	 if(!printf("Hello "))
		  printf("Hello ");
	 else
		  printf("world\n");
}
 
Old 05-20-2004, 03:16 PM   #6
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Thats gay
 
Old 05-20-2004, 03:23 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by The_Nerd
Thats gay
Huh?
 
Old 05-20-2004, 03:26 PM   #8
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Rep: Reputation: 30
if(1)
printf("Hello");
else
printf("world");

That would be one way of doing it.
Or


main() {
int x = 0;

if(x)
printf("Hello");
else
printf("world");

}


Just my two bits.
 
Old 05-20-2004, 04:24 PM   #9
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Quote:
Originally posted by WindowsBurner
if(1)
printf("Hello");
else
printf("world");

That would be one way of doing it.
Or


main() {
int x = 0;

if(x)
printf("Hello");
else
printf("world");

}


Just my two bits.
Neither of those would work.
 
Old 05-20-2004, 07:17 PM   #10
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Well if you guys are going to be gay... then...

Code:
int main()
{
	int i;
	for (i=0;i<2;i++)
		if (!i)
			printf("Hello");
		else
			printf(" World!");
}
There... Thats my two cents! Btw it is just as valid as any other posted here.
 
Old 05-20-2004, 07:52 PM   #11
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 45
Quote:
Originally posted by The_Nerd
Well if you guys are going to be gay... then...

There... Thats my two cents! Btw it is just as valid as any other posted here.
as funny as you think you might be OR just plain happy and excited to be on a forum like this because of whatever reasons you might have, people don't use this place for showing their smarts.
if you can help/contribute in a positive way, do so ... if not, go play with your toys.

the moderators might not be happy with this post of mine, and u werent talkin to me, but i just hate self-important fsckheads.

there ... that's my two cents! btw it is just as valid as your two posts here. :rollseyes:
 
Old 05-21-2004, 06:25 AM   #12
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Rep: Reputation: 30
Quote:
Neither of those would work.
What compiler are you using? Cause that works for me.
 
Old 05-21-2004, 07:54 AM   #13
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
Windowsburner, your examples should compile on any sane C compiler, but they are not solutions to the puzzle at all.
Code:
$ ./burner00
Hello$

$ ./burner01
world$

$ ./burner00 ; echo -n " " ; ./burner01 ; echo
Hello world
$
No compiler, given your source files, should produce an executable which, when run, outputs "Hello world\n". In your first example, the first "if" succeeds, and the "else" is thus skipped. The opposite happens in your second example...

Last edited by rkef; 05-21-2004 at 07:59 AM.
 
Old 05-21-2004, 10:32 AM   #14
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Quote:
Originally posted by h/w
as funny as you think you might be OR just plain happy and excited to be on a forum like this because of whatever reasons you might have, people don't use this place for showing their smarts.
if you can help/contribute in a positive way, do so ... if not, go play with your toys.

the moderators might not be happy with this post of mine, and u werent talkin to me, but i just hate self-important fsckheads.

there ... that's my two cents! btw it is just as valid as your two posts here. :rollseyes:
Funny??? That was a perfectly valid, and perfectly fine contribution. It is a puzzle is it not? It is not like some guy has to have a correct answer quickly or he will get fired. Get off it man, this post is just for fun.

The only reson I call it gay is because the answers supassed the origional question. The origional question could never be true. But with what the people here did, you could just have easly got rid of the if statement all together and just did a printf!

Dood, I think you are way to tight. Loosen up a bit ok. Join in on our fun, just don't mock it.

Btw... That is now 4 cents.
 
Old 05-21-2004, 10:33 AM   #15
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
By the Way WindowsBurner... You need a new compiler bad!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GTK puzzle Chuck23 Fedora 9 08-24-2004 03:04 AM
Partition puzzle davoman Linux - General 7 04-26-2004 08:35 PM
Printer Puzzle seanmaggs LinuxQuestions.org Member Success Stories 0 03-30-2004 05:04 PM
dhcpd puzzle sureshot! Linux - Networking 3 10-22-2003 05:54 AM
rpm puzzle jbmcmillan Linux - Software 3 10-16-2003 02:42 PM


All times are GMT -5. The time now is 01:27 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration