tags:

views:

266

answers:

8
+3  A: 
CONDITION = printf("Hello") == 0;
Daniel Moura
Makes no sense for me!? You alter the code structure, is this what was intended?
rubber boots
+2  A: 

This is a bit like cheating but...

void main()
{
         if (printf("Hello ") == 0)
               printf("Hello ");
         else
               printf("World");
}
nico
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
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.
Steve314
@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
+32  A: 
 if(fork() == 0)
    ...
rubber boots
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.
Kristopher Johnson
+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
You could fix the ordering problem by making the condition `!fork() || !wait(NULL)`
caf
A: 

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");
iniju
A: 
#include <stdio.h>
main()
{
  if (printf("Hello "),0)
    printf("Hello ");
  else
    printf("World");
  return 0;
}
+16  A: 

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");
}
x4u
+1 for being evil. I like it.
Zan Lynx
+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) ;-)
rubber boots
You can even do this while respecting that you only modify CONDITION. Set CONDITION equal to newline, #define else, newline, 1
Zan Lynx
+1  A: 

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.

Potatoswatter
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
@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.
Potatoswatter
A: 

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.

RBerteig
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
@x4y, sneaky. I like it.
RBerteig