Saturday, June 18, 2011

Interview Question - IF condition to print "Hello World" in C

What should be the so that the following code snippet prints ”Hello World”?


if( <condition> )
  printf ("Hello");
else
  printf("World");
Solution 1 - Using fork() system call

#include <stdio.h>

int main(void)
{
  if(fork())
    printf ("Hello ");
  else
    printf("World");

  return 0;
}
Solution 2 - Using return value of printf() function

#include <stdio.h>

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

  return 0;
}
Written by

1 comment:

  1. solution3:
    main()
    {
    if(1)
    printf("Hello");
    goto ELSE;
    else
    {
    ELSE:
    printf("world");
    }
    }

    ReplyDelete