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:
#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.
Recommended Posts:
- Count elements in a vector that match a target value or condition
- How to print a variable name in C?
- How to print % using printf()?
- How to Read and Print an Integer value in C++
- Print 1 2 3 infinitely using threads in C
- How to Read and Print an Integer value in C
- How will you print numbers from 1 to 100 without using loop? | Set-2
- Print a long int in C using putchar() only
- 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 will you print numbers from 1 to 100 without using loop?
- Print calendar for a given year in C++
- Print Hello World without semicolon in C/C++
- How to print dimensions of multidimensional array in C++



