Tutorials  Articles  Notifications  Login  Signup


AS

Anoop Singh

SDE 1 at Amazon July 27, 2020, 12:11 p.m. ⋅ 872 views

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:

  1. Using If condition
  2. Using While condition
  3. Using switch case
  4. 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)) 
  { 
  } 
} 

 



HackerFriend Logo

Join the community of 1 Lakh+ Developers

Create a free account and get access to tutorials, jobs, hackathons, developer events and neatly written articles.


Create a free account