Print Hello World without semicolon in C/C++
Last Updated :
13 Mar, 2023
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:
#include <iostream>
int main()
{
if (std::cout << "Hello World " )
{
}
}
|
Output:
Hello World
- Using switch statement:
#include <stdio.h>
int main()
{
switch ( printf ( "Hello World " ))
{
}
}
|
Output:
Hello World
- Using macros
#include <stdio.h>
#define GEEK printf("Hello World")
int main()
{
if (GEEK)
{
}
}
|
Output:
Hello World
- Using loops (while and for) : Here, important thing to note is using !(not operator) in while loop to avoid infinite loop.
#include<iostream>
int main()
{
while (!(std::cout << "Hello World" ))
{ }
}
|
Hello World
Related Article: How to print a semicolon(;) without using semicolon in C/C++?
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...