C program to print "HackersFriend" without using a semicolon
We can print anything is C without using semicolon, there are many ways to do it. We'll show implementations of following ways:
- Using If condition
- Using While condition
- Using switch case
- and Using Macros
But, to understand actually, how it works, let's understand working of printf() first. Here is the prototype of printf().
int printf( const char *format , ...)
Parameters of printf()
- format - String that needs to be written / printed to stdout.
- … : Three dots are called ellipses. It indicates the variable number of arguments as per the format string.
Return type
- int - The number of characters written to stdout
Here we see, printf returns an integer value, so, we can use it inside any condition. Because in C, integer values are valid inside any condition. That's the trick to print anything without using semicolon.
Using if condition
#include<stdio.h>
int main()
{
if (printf("HackersFriend") )
{ }
}
Using while condition
#include<stdio.h>
int main()
{
while (!printf( "HackersFriend" ))
{ }
}
Using switch cases
#include<stdio.h>
int main()
{
switch (printf("HackersFriend" ))
{ }
}
Using macros
#include<stdio.h>
#define HFPRINT printf("HackersFriend")
int main()
{
if (HFPRINT)
{ }
}
Print semicolon without using semicolon
Sometimes, people might trick you to print semicolon, without using semicolon. Solution here is to use ASCII value of semicolon, it is 59.
#include<stdio.h>
int main()
{
if (printf("%c", 59))
{
}
}