Alex Rivera | Logout

What's the "condition" in C interview test?

Asked 2008-08-28T19:37:33.343
30

Would it be possible to print Hello twice using single condition?

if  "condition"
  printf ("Hello");
else
  printf("World");
Edit
Report

4 Answers

85
if ( printf("Hello") == 0 )
    printf ("Hello");
else
    printf ("World");

:-)

answered 2008-08-28T19:41:32.740
6
#define CONDITION (0) if (0) {} else

or some such.

If you see such a question on an interview, run away as fast as you can! The team that asks such questions is bound to be unhealthy.

Edit - I forgot to clarify - this relies on "else" being matched with closest open "if", and on the fact that it's written as "if CONDITION" rather than if (CONDITION) - parenthesis would make the puzzle unsolvable.

answered 2008-08-28T19:45:53.260
0
if(printf("Hello") == 1)
    printf("Hello")
else
    printf("World")
answered 2008-08-28T19:41:10.200
0

use a goto, one of the single most underused keywords of our day

answered 2009-11-21T06:32:52.980

Your Answer