Thread: C puzzle

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    C puzzle

    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");

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Sounds like a weird puzzle, but if the following is your condition, it'll print "Hello World":

    Code:
    (printf("Hello ") != 6)
    Or if you wanted no spaces:

    Code:
    (printf("Hello") != 5)
    I don't think that's what the writer(s) of the challenge had in mind, though.

    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.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    72
    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

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    11
    how about this....
    Code:
    if(!printf("hello"))
    
    printf("hello");
    
    else
    
    printf("world);

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by kky2k View Post
    how about this....
    Code:
    if(!printf("hello"))
    
    printf("hello");
    
    else
    
    printf("world);
    Isn't that what MacGyver suggested :\

    Quote Originally Posted by printf return value
    printf - On success, the total number of characters written is returned.
    So 5 != 5 is the same as !5
    Last edited by zacs7; 07-09-2007 at 08:48 PM.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by zacs7 View Post
    So 5 != 5 is the same as !5
    but it could be < 5.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Only if somehow printf() fails :|

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by King Mir View Post
    If you want the expression to always evaluate to false you should do this:
    if(printf("Hello "),false)
    You should? Really!??!

    No.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by zacs7 View Post
    Only if somehow printf() fails :|
    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.

    Quote Originally Posted by King Mir View Post
    If you want the expression to always evaluate to false you should do this:
    if(printf("Hello "),false)
    Inventing additions to the C language today, are we?

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by MacGyver
    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.
    True, it says print "Hello World" not "ello World" or something along those lines - it's Hello World or nothing.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Inventing additions to the C language today
    well... this code WILL work:
    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

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Quote Originally Posted by zacs7 View Post
    You should? Really!??!

    No.
    Why not? The puzzle is goofy anyway. Is there some reason this solution is bad?

    EDIT: Heh. Forgot which board I was on again. Okay, so "#define false 0"

  14. #14
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    381
    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.

  15. #15
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by MacGyver View Post
    Inventing additions to the C language today, are we?
    C99 does have the boolean type, and defines true and false in stdbool.h

    If you consider a standard header to be part of the language, which you must since you're using printf(), then false is perfectly OK.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The puzzle again...Swapping elements of 2D array
    By crazygopedder in forum C Programming
    Replies: 44
    Last Post: 11-05-2008, 01:53 PM
  2. Replies: 12
    Last Post: 06-06-2008, 05:26 PM
  3. Crossword Puzzle Program
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2006, 11:08 PM
  4. Solution to Google Puzzle 3,3,8,8=24
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-01-2006, 09:12 AM