Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Condition To Print “HelloWorld”

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

What should be the “condition” so that the following code snippet prints both HelloWorld!
 

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

Method 1: using logical NOT(!)
 

c




#include<stdio.h>
int main()
{
    if(!printf("Hello"))
        printf("Hello");
    else
        printf("World");
    getchar();
}       

Explanation: Printf returns the number of character it has printed successfully. So, following solutions will also work
if (printf(“Hello”) < 0) or 
if (printf(“Hello”) < 1) etc 
Method 2: Using fork() 
 

c




#include<stdio.h>
#include<unistd.h>
int main()
{
    if(fork())
        printf("Hello");
    else
        printf("World");
    getchar();
}       

This method is contributed by Aravind Alapati.
Please comment if you find more solutions of this.
 

My Personal Notes arrow_drop_up
Last Updated : 16 Jul, 2022
Like Article
Save Article
Similar Reads