C if condition Puzzle

What's the "condition" so that the following code
snippet prints both HelloWorld !

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

Questions by ranjan4unitt

Editorial / Best Answer

kiranb_rvce  

  • Member Since Jun-2008 | Jun 6th, 2008


if ( ! ( printf("Hello ") ) )
printf("Hello");
else
printf("World");

Showing Answers 1 - 8 of 8 Answers

mmpcse

  • Jun 1st, 2008
 

if((printf("Hello") > 0 ? 0 : 1))
        printf("Hello");
    else
        printf("World!n");

if you want World!Hello, then change 0 : 1 to 1 : 0

Happy Coding :)

fazil.vp

  • Apr 24th, 2009
 

/* Solution 1: */

int main(int argc, char* argv[])
{
if( argc == 2 || main( 2, NULL ) )
{
printf("Hello ");
}
else
{
printf("Worldn");
}
return 0;
}

/* Solution 2 (Only for Unix and Linux): */

int main(int argc, char* argv[])
{
if( !fork() )
{
printf("Hello ");
}
else
{
printf("Worldn");
}
return 0;
}

  Was this answer useful?  Yes

Florian F

  • Nov 17th, 2017
 

Condition can be empty

Code
  1. #include <stdio.h>

  2. #define printf(s) (!printf(s))

  3. int main(int argc, char *argv[]){

  4.     if  

  5.         printf("Hello ");

  6.     else

  7.         printf("world!

  8. ");

  9. }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions