Would it be possible to print Hello
twice using single condition
?
if \"condition\"
printf (\"Hello\");
else
printf(\"World\");
The if
statement executes one or the other of the controlled statements (both printf
in your example). No matter what you use for condition
, that snippet will either print "Hello", or "World", but never both.
Edit: Okay, so it's a trick question and you can put whatever you like in the condition (including a call to an entire other function that does anything you want). But that's hardly interesting. I can't believe I got downmodded for giving a correct answer.
if (printf("hello") & 0)
{
printf("hello");
}
else
{
printf("world");
No need to bother about the return value of printf.
Two possible Solutions without using printf statements :-
First :-
#include <stdio.h>
int
main(void)
{
if (!stdin || (stdin = 0, main()))
printf("hello");
else
printf("world");
return 0;
}
Second
#include<stdio.h>
void main()
{
if (1
#define else if (1)
)
{
printf("hello");
}
else
{
printf("world");
}
}
Reference :- Link1 , Link2
If it is on Unix:
if (fork())
printf ("Hello");
else
printf("World");
Ofcoures that doesn't guarantee the order 0f the prints
use a goto, one of the single most underused keywords of our day
So... you want to execute the code inside the if block... and the code inside of the else block... of the same if/else statement? Then... you should get rid of the else and stick taht code in the if.
if something
do_this
do_that
end
The else statement is designed to execute only if the if statement is not executed and vice-versa, that is the whole point. This is an odd question...