C Program to Print Hello World Without Using Semicolon

There are many ways to write a C program to print hello world to the screen without using a semicolon.
1) Using Conditional statements
2) Using Macros
3) Using command-line arguments

Using Conditional statements

The string is the group of characters, and to display the group of characters printf and puts function can be used. Using conditional statements and printf or puts we can write C program to print hello world without using a semicolon.

Program1:- Using printf and if conditional statement

We can write a C program to print hello world without using a semicolon, with printf and if statement. The printf function displays the string “Hello World” and returns the number of characters in the string so if the condition becomes true. But our task is completed hence close the if statement using {}.

#include<stdio.h>
int main()
{
  if(printf("Hello World")){}
  return 0;
}

Output:-

Hello World

Program2:- Using puts and if conditional statement

#include<stdio.h>
int main()
{
  if(puts("Hello World")){}
  return 0;
}

Program3:- Using printf and while conditional statement

The printf or puts function can be used as a condition of while conditional statement.

#include<stdio.h>
int main()
{
  while(!printf("Hello World")){}
  return 0;
}

Program4:- Using puts and while conditional statement

#include<stdio.h>
int main()
{
  while(!puts("Hello World")){}
  return 0;
}

Program5:- Using printf and switch conditional statement

#include<stdio.h>
int main()
{
  switch(printf("Hello World")){}
  return 0;
}

Program6:- Using puts and switch conditional statement

#include<stdio.h>
int main()
{
  switch(puts("Hello World")){}
  return 0;
}

Using Macros C Program to Print Hello World without Using a Semicolon

Macros can also be used to print the string “Hello World” to the screen without using a semicolon. We need to define a macro using printf or puts and use this macro within conditional statements.

#include<stdio.h>
#define SHOW printf("Hello World")
int main()
{
  if(SHOW){}
  return 0;
}

Output:-

Hello World

In this program, we used macros with the printf function. Later this macro is used inside if conditional statement. We can also use this macro inside a while, or switch conditional statement.

while(!SHOW){}
switch(SHOW){}

Similarly, in place of the printf function, we can use puts function.

#include<stdio.h>
#define SHOW puts("Hello World")
int main()
{
  while(!SHOW){}
  return 0;
}

Using Command-line Arguments

The main function has two arguments, these are called command-line arguments.
int main(int argc, char *argv[ ])
Here argc takes the number of commands and argv store those commands entered by the end-user.

#include<stdio.h>
int main(int argc, char *argv[printf("Hello World")])
{}

Output:-

Hello World

We can also use the below program,

#include<stdio.h>
int main(char *argv[printf("Hello World")])
{}

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *