I found this puzzle on C but couldnt solve..can some one give me the answer please
what goes in the condition to print Hello World
Code:if "condition" printf ("Hello"); else printf("World");
I found this puzzle on C but couldnt solve..can some one give me the answer please
what goes in the condition to print Hello World
Code:if "condition" printf ("Hello"); else printf("World");
Sounds like a weird puzzle, but if the following is your condition, it'll print "Hello World":
Or if you wanted no spaces:Code:(printf("Hello ") != 6)
I don't think that's what the writer(s) of the challenge had in mind, though.Code:(printf("Hello") != 5)
Think I found the challenge: http://paonethestar.wordpress.com/20...ing-puzzles-2/
Perhaps they did want a solution like this.
Last edited by MacGyver; 07-09-2007 at 03:50 AM.
the only thing that i can think about is that when the condition "holds". In other words, the condition is true or represent a digit that is not '0'
proud to be from aui www.aui.ma and also a great elton john's fan
how about this....
Code:if(!printf("hello")) printf("hello"); else printf("world);
Last edited by zacs7; 07-09-2007 at 08:48 PM.
Only if somehow printf() fails :|
If you want the expression to always evaluate to false you should do this:
if(printf("Hello "),false)
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Exactly. If printf() fails, then you probably shouldn't expect the next printf() to succeed. That means no part of the message should be printed. There is no reason to assume printf() will fail for this example since the challenge was to get "hello world" to the screen by a condition in a snippet of code that already relies upon printf() working successfully.
Therefore, I think it safe to assume for this example that printf() will not fail, for the sake of the challenge.
Inventing additions to the C language today, are we?
True, it says print "Hello World" not "ello World" or something along those lines - it's Hello World or nothing.Originally Posted by MacGyver
well... this code WILL work:Inventing additions to the C language today
Code:#include <stdio.h> int main() { if(printf("Hello "),0) { } else printf( "World!\n"); return 0; }
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
If you want to print "Hello" and "world", an if-else construct, by definition, isn't going to achieve that.
It will print one or the other.
EDIT: sorry, reading the thread properly, I just realised the puzzle. As has been said, make the if fail by testing for an amount of chars not equal to 6.
Last edited by samGwilliam; 07-11-2007 at 12:27 PM.
There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.