What should be the “condition” so that the following code snippet prints both HelloWorld!
if "condition"
printf ("Hello");
else
printf("World");
Method 1:
#include<stdio.h> int main() { if(!printf("Hello")) printf("Hello"); else printf("World"); getchar(); } |
chevron_right
filter_none
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()
#include<stdio.h> #include<unistd.h> int main() { if(fork()) printf("Hello"); else printf("World"); getchar(); } |
chevron_right
filter_none
This method is contributed by Aravind Alapati.
Please comment if you find more solutions of this.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count elements in a vector that match a target value or condition
- How will you print numbers from 1 to 100 without using loop?
- Write a C program to print "Geeks for Geeks" without using a semicolon
- Change/add only one character and print '*' exactly 20 times
- Write a C macro PRINT(x) which prints x
- How to print % using printf()?
- Print "Even" or "Odd" without using conditional statement
- Print 1 to 100 in C++, without loop and recursion
- Program to print last 10 lines
- How to print "GeeksforGeeks" with empty main() in C, C++ and Java?
- Print a long int in C using putchar() only
- Print substring of a given string without using any string function and loop in C
- How to print range of basic data types without any library function and constant in C?
- Print individual digits as words without using if or switch
- Print 2D matrix in different lines and without curly braces in C/C++?
- How to print size of array parameter in C++?
- Print calendar for a given year in C++
- Print "Hello World" in C/C++ without using any header file
- Program to print the diamond shape
- Program to print Happy Birthday

