Printf Puzzle : Guess what should X be replaced with so as to get output as “Hello World“
#include<stdio.h>
void main()
{
if(X)
{
printf("Hello");
}
else
{
printf("World");
}
}
Way1 : Output
#include<stdio.h>
void main()
{
if(printf("Hello")-5)
{
printf("Hello");
}
else
{
printf("World");
}
}
Explanation of the Program :
- Printf Returns Number of Characters Printed On Screen
- Printf Inside “If Statement” will print “Hello” and returns Integer(number of Charcaters Printed)
- Subtract 5 from That number so We will get if(0) which is False Condition.
- Inside “else” block we are printing “world” .
Way 2 : Output :
#include<stdio.h>
void main()
{
if(!printf("Hello"))
{
printf("Hello");
}
else
{
printf("World");
}
}
Way 3 : Output :
#include<stdio.h>
void main()
{
if((printf("Hello") > 0 ? 0 : 1))
{
printf("Hello");
}
else
{
printf("World");
}
}