C program to print a string without using semicolon in the printf

C program to print a string without using semicolon in the printf :

In this tutorial, we will learn how to print a string using printf and without using a semicolon. We will show you different approaches below :

Using a for loop :

#include

int main(){
	int i;
	for(i=0 ; !printf("Hello World\n");i++){

	}
}

This program will print the string Hello World. Because, the loop will first try to run, since the value of !printf() is always false, it will print the string and exit the loop.

Using a while loop :

#include

int main(){
	while(!printf("Hello World\n")){

	}
}

Using if :

#include

int main(){
	if(!printf("Hello World\n")){

	}
}

Using switch :

#include

int main(){
	switch(printf("Hello World\n")){

	}
}

This question is asked mainly in programming interviews. Each of these above process uses the same concept .

Leave a Reply