4

Possible Duplicate:
What’s the “condition” in C interview question?

void main()
{
         if(CONDITION)
               printf("Hello ");
         else
               printf("World");
}

Replace CONDITION with such a condition that both the printf statements get executed. You can't have a loop or other things in main() I found this code online,i am trying to think the logic behind it but i cant. Please help me out Thanks

7
  • 4
    There is a reason it's called if / else
    – Kirk Woll
    Aug 12, 2010 at 21:29
  • 2
    Is this homework? If so, please tag it as such. Aug 12, 2010 at 21:30
  • 3
    Are you sure your implementation has documented void main() as being valid? Otherwise, that isn't a C program. Use int main() by preference, as that's valid everywhere. Aug 12, 2010 at 21:34
  • Ha! Thats a very good question. This way you can tell experienced programmers from newbies. I'll copy that one. Thanks! Aug 12, 2010 at 21:36
  • 2
    I thought i saw this question already and found it again: stackoverflow.com/questions/33199/… Aug 12, 2010 at 21:38

8 Answers 8

35
 if(fork() == 0)
    ...
3
  • 2
    There is a race condition here, in that you don't know which printf would be executed first, but the rules just say "both the printf statements get executed", without any restriction on order, so it looks good to me. Aug 12, 2010 at 21:35
  • +1, nice! It's worth noting that this only works on Unix, but I bet that's what whoever came up with the question was looking for...
    – Jim Lewis
    Aug 12, 2010 at 21:39
  • 3
    You could fix the ordering problem by making the condition !fork() || !wait(NULL)
    – caf
    Aug 12, 2010 at 22:32
20

While I really like the idea behind the answer of rubber boots I think there might be a more trivial answer. The description explicitly states that you may not have code inside main() but what about having a extra line outside?

#define else

void main()
{
     if(1)
           printf("Hello ");
     else
           printf("World");
}

Update Here is an alternative that was suggested byZan Lynx in the comments. It only adds code between the parentheses around CONDITION:

void main()
{
     if(1
#define else
       )
           printf("Hello ");
     else
           printf("World");
}
3
  • 4
    +1 for being evil. I like it.
    – Zan Lynx
    Aug 12, 2010 at 21:46
  • +1 very good idea. I suspect this won't be the answer intended but this is more a "C-like" answer than the fork-thing (which is too verbose) ;-) Aug 12, 2010 at 21:51
  • 3
    You can even do this while respecting that you only modify CONDITION. Set CONDITION equal to newline, #define else, newline, 1
    – Zan Lynx
    Aug 12, 2010 at 21:56
3

This is a bit like cheating but...

void main()
{
         if (printf("Hello ") == 0)
               printf("Hello ");
         else
               printf("World");
}
3
  • Firstly Thanks a lot. And well its not exactly cheating if you give your best but still cant crack it..Thanks for the help.Appreciate it
    – Ritik
    Aug 12, 2010 at 21:42
  • 3
    It's cheating - it doesn't run the printf("Hello ") that it's meant to run, and the substitute printf even forgets to include the space, meaning a user-visible bug.
    – user180247
    Aug 13, 2010 at 1:04
  • 1
    @Steve314: thanks for pointing that out, didn't notice the space, I corrected it. I agree, it's cheating, but the question itself it's stupid (no offense meant, Ritik): if you want to execute two statement you don't use an if/else construct. At the end it does the job, and that's what matters :D
    – nico
    Aug 13, 2010 at 6:11
3
CONDITION = printf("Hello") == 0;
2
  • Makes no sense for me!? You alter the code structure, is this what was intended? Aug 12, 2010 at 21:33
  • @rubberboots he was suggesting to replace the condition with this expression
    – zoran404
    Oct 6, 2014 at 0:13
3

Here's another approach. It's not as good as fork in that it tends to work only half the time (hence not completely solving the problem), but it is better in that the message never gets reversed.

#include <stdio.h>

int main() {
        if ( ftell( stdout ) % 2 || ( printf( " " ), main() ) )
                printf( "Hello " );
        else
                printf( "World\n" );
}

It first queries stdout to see how much has been printed and whether the number of characters is odd. If so, it then prints a single character to reverse the parity and recurses. The recursive call sees an even number of characters and prints "hello" and returns 0. The 0 sends the top call into the else, printing "world."

The number of characters in the terminal must be odd for it to work.

2
  • 1
    I like the idea of calling main() another time. I think you could even make it deterministically print "Hello World" with if( *"x"++ == 'x' ? main(), 0 : 1 ). This might need some compiler switch on recent compilers to allow the modification of string literals.
    – x4u
    Aug 13, 2010 at 2:39
  • @x: Thanks, I was racking my brain. I believe there are legitimate global variables in the C standard library that can be used, but I can't think of any… too rusty. If he just declared argc and argv, it would be so much easier. Aug 13, 2010 at 2:46
2

Not exactly what you asked, but the following would at least print the same result as if when both printf were executed:

     if(printf("Hello ") & 0)
           printf("Hello ");
     else
           printf("World");
2

Leaping straight off the cliff pointed out by Potatoswatter, I offer the following:

#include <stdio.h>
#include <errno.h>

int main()
{
    if ((errno == 42) || (errno=42, main()))
    printf("hello, ");
    else
    printf("world.");
   return 0; 
}
C:\...>gcc -Wall q3472196.c

C:\...>a
hello, world.
C:\...>

I did have to declare main and give it a return value to shut up a couple of warnings. There might even be something suitably evil defined in stdio.h itself so the we don't have to declare errno.

2
  • How about if( stdin == 0 || ( stdin = 0, main(), 0 ) ) ? This gets rid of the errno.h include and doesn't need a return statement in main(). However stdio.h would still need to be included although printf() would also compile without it.
    – x4u
    Aug 13, 2010 at 8:34
  • @x4y, sneaky. I like it.
    – RBerteig
    Aug 13, 2010 at 8:58
0
#include <stdio.h>
main()
{
  if (printf("Hello "),0)
    printf("Hello ");
  else
    printf("World");
  return 0;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.