Condition To Print “HelloWord”

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

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

Method 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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()

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


This method is contributed by Aravind Alapati.

Please comment if you find more solutions of this.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


7


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.