Print Hello World without semicolon in C/C++
Every statement in C++ must end with a semicolon as per basics. However, unlike other languages, almost all statements in C++ can be treated as expressions. However there are few scenarios when we can write a running program without semicolon.
If we place the statement inside an if/switch/while/macro statement with a blank pair of parentheses, we don’t have to end it with a semicolon. Also, calling a function that returns void will not work here as void functions are not expressions. We can although use a comma operator, with any value in the right hand side of the operator.
Examples:
- Using if statement:
// CPP program to print// Hello World without semicolon// using if statement#include <iostream>intmain(){if(std::cout <<"Hello World "){}}chevron_rightfilter_noneOutput:
Hello World
- Using switch statement:
// CPP program to print// Hello World without semicolon// using switch statement#include <stdio.h>intmain(){switch(printf("Hello World ")){}}chevron_rightfilter_noneOutput:
Hello World
- Using macros
// CPP program to print// Hello World without semicolon// using macros#include <stdio.h>#define GEEK printf("Hello World")intmain(){if(GEEK){}}chevron_rightfilter_noneOutput:
Hello World
- Using loops (while and for) : Here, important thing to note is using !(not operator) in while loop to avoid infinite loop.
// CPP program to print// Hello World without semicolon// using if statement#include<iostream>intmain(){while(!(std::cout <<"Hello World")){ }// for loop can also be used// where testing condition has cout statement// for (;!(std::cout << "Hello World");)// { }}chevron_rightfilter_noneHello World
Related Article: How to print a semicolon(;) without using semicolon in C/C++?
This article is contributed by Avni Nargwani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- How to print a semicolon(;) without using semicolon in C/C++?
- C Program to print numbers from 1 to N without using semicolon?
- Write a C program to print "Geeks for Geeks" without using a semicolon
- Print "Hello World" in C/C++ without using any header file
- Print "Hello World" with empty or blank main in C++
- Writing first C++ program : Hello World example
- OpenMP | Hello World program
- Beginning Java programming with Hello World Example
- How to print a variable name in C?
- Program to print Hut
- How to print % using printf()?
- Program to print the given Z Pattern
- Pattern to print X in a rectangular box
- Program to print the Sum of series -1 + 2 + 11 + 26 + 47 +.....
- Print 1 2 3 infinitely using threads in C



