C++ Hello World Program with Step-by-Step Guide
The "Hello, World!" program is the simplest and most traditional way to begin learning any new programming language.
In this tutorial, you'll learn how to write your first C++ Hello World Program with step-by-step comments and beginner-friendly formatting.
Whether you're searching for:
-
Hello World for C++
-
C++ Hello World Program
-
C++ language Hello World example
You're at the right place!
Introduction to Hello World in C++
A "Hello, World!" program simply prints a greeting message on the screen. It helps you understand the basic structure of the C++ language such as:
-
Headers (
#include <iostream>
) -
Main function (
int main()
) -
Output statement (
cout
)
Basic C++ Hello World Program
// HELLO WORLD PROGRAM IN C++ USING MAIN FUNCTION
#include <iostream>
using namespace std;
// It's the main function of the program
int main()
{
// Step-1 Display a label message
cout << "C++ HELLO WORLD PROGRAM" << endl;
// Step-2 Print the actual Hello World message
cout << "HELLO, WORLD!" << endl;
return 0;
}
Output
C++ HELLO WORLD PROGRAM
HELLO, WORLD!
C++ Hello World Program using Function
// HELLO WORLD PROGRAM IN C++ USING USER-DEFINED FUNCTION
#include <iostream>
using namespace std;
// Step-1 Create a function to display Hello World
void displayMessage()
{
cout << "HELLO, WORLD!" << endl;
}
// It's the main function of the program
int main()
{
// Step-2 Display a label
cout << "C++ HELLO WORLD PROGRAM" << endl;
// Step-3 Call the function to show the message
displayMessage();
return 0;
}
C++ Hello World using Loop
// HELLO WORLD PROGRAM IN C++ USING FOR LOOP
#include <iostream>
using namespace std;
// It's the main function of the program
int main()
{
// Step-1 Display a label
cout << "C++ HELLO WORLD PROGRAM" << endl;
// Step-2 Use loop to print message 3 times
for (int i = 0; i < 3; i++)
{
cout << "HELLO, WORLD!" << endl;
}
return 0;
}
C++ Hello World using Array
// HELLO WORLD PROGRAM IN C++ USING CHARACTER ARRAY
#include <iostream>
using namespace std;
// It's the main function of the program
int main()
{
// Step-1 Create an array to store the message
char message[] = "HELLO, WORLD!";
// Step-2 Display a label
cout << "C++ HELLO WORLD PROGRAM" << endl;
// Step-3 Print the message using array
cout << message << endl;
return 0;
}
C++ Hello World using if Condition
// HELLO WORLD PROGRAM IN C++ USING IF CONDITION
#include <iostream>
using namespace std;
// It's the main function of the program
int main()
{
// Step-1 Display a label
cout << "C++ HELLO WORLD PROGRAM" << endl;
// Step-2 Use if condition to print the message
bool show = true;
if (show)
{
cout << "HELLO, WORLD!" << endl;
}
return 0;
}
FAQs
Q1. Is using main()
function mandatory in C++?
Yes, every C++ program must have a main()
function as the entry point.
Q2. What does cout
mean in C++?
cout
is used to display output to the console. It's part of the iostream
library.
Q3. Can we print Hello World without using main()
?
No. C++ strictly requires main()
to execute the program.