Hello World

A simple C++ program that prints "Hello World" on the screen and introduces basic C++ concepts for beginners.

BeginnerTopic: Basic Programs
Back

C++ Hello World Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
// This line allows us to use input and output (like printing on the screen)
                  #include <iostream>
                  using namespace std;
                  
                  // main() function: this is where every C++ program starts running
                  int main() {
                      // This line prints "Hello World" on the screen
                      cout << "Hello World";
                  
                      // This line tells the computer that the program ended successfully
                      return 0;
                  }
Output
Hello World

Understanding Hello World

This is the famous "Hello World" program in C++. It is usually the first program you write when you start learning C++.

The main purpose of this program is to show a simple message on the screen: Hello World.

Even though the program looks small, it contains many important concepts of C++ that you will use again and again.

---

What does this program do?

It starts the C++ program.
It uses the C++ library for input and output.
It prints the text "Hello World" on the screen.
It then ends the program properly.

---

1. Preprocessor Directive: #include <iostream>

Line in the code:

The word "include" means "add this file before compiling the program".
"iostream" is a built-in C++ library.
This library allows us to do input and output operations.
Input means taking data from the user.
Output means showing data on the screen.
In this program, we use "cout", which is defined inside the iostream library.

Without this line, the computer does not know what "cout" is, and the program will not compile.

---

                  #include <iostream>

2. Header Files (simple idea)

A header file is like a toolbox.
Each header file has tools (functions, classes, etc.) that we can use in our program.
When we write #include <iostream>, we are saying:

"Please attach the toolbox that has input and output tools."

Examples:

iostream → used for input and output (cout, cin)
string → used to work with text (strings)
math-related headers → used for mathematical functions

In older C programs, many header files had ".h" at the end, like stdio.h.

In modern C++, many standard headers like iostream do not have .h.

---

3. Namespace: using namespace std;

Line in the code:

C++ stores many built-in names like cout, cin, string, etc., inside a group called "std".
This group is called a "namespace".
The line "using namespace std;" tells the compiler:

"I want to use the names inside the std group directly."

Without this line, we would have to write "std: :cout" instead of "cout".

So:

With using namespace std; → we can write:

cout << "Hello World";

Without it → we must write:
std: :cout << "Hello World";

For beginners, using namespace std; makes the code easier to read and write.

---

                  using namespace std;

4. Main Function: int main() { ... }

Line in the code:

}

Every C++ program must have a function named "main".
The main function is the starting point of the program.
When you run the program, the computer starts reading and executing code from inside main.

Let's break it:

"int" before main means that this function returns an integer (a whole number).
The round brackets () after main mean it is a function.
The curly braces { and } mark the beginning and the end of the main function.
All the code that should run when the program starts is written between these braces.

In simple words:

main() is the entry gate of your C++ program.

---

                  int main() {
                      // code
                      return 0;

5. Comments in the Code

Examples in the code:

A comment starts with // in C++.
Anything written after // on that line is ignored by the compiler.
Comments do not affect how the program runs.
They are written only for humans to understand the code better.

Why are comments useful?

They make the code easier to read.
They explain what each part of the code is doing.
They help beginners remember concepts.
Good habit: Write comments to describe your logic, especially when you are learning.

---

                  // This line allows us to use input and output (like printing on the screen)

6. Output Statement: cout << "Hello World";

This is the most important line in this program:

"cout" means "character output". It is used to print text on the screen.
The symbol << is called the insertion operator. It sends the text on its right side to the output stream.
"Hello World" is a string (text) written inside double quotes.
The semicolon ; at the end tells the compiler:

"This statement ends here."

So this line simply means:

"Send the text Hello World to the screen."

If you change the text inside the double quotes, the output will change. For example:

cout << "Welcome to C++";

Output will be:

Welcome to C++

---

                  cout << "Hello World";

7. Return Statement: return 0;

Line in the code:

This line is written at the end of the main function.
It sends a value back to the operating system.
In this case, it returns 0, which usually means:

"The program ended successfully without any errors."

If main returns 0:

It is a signal that everything worked fine.

In simple words:

return 0; tells the computer:

"I am done. The program ran correctly."

---

                  return 0;

8. Summary of the Program Flow

1.The program includes the iostream header to use input and output.
2.It uses the standard namespace to avoid writing std:: again and again.
3.The main function starts. This is where the program begins.
4.Inside main, cout prints "Hello World" on the screen.
5.The return 0; line ends the program successfully.

---

Why is this program important? (For beginners and SEO)

It is the first step in learning C++ programming.
It teaches you:
How to write a basic C++ program.
How to use header files.
What main() is.
How to print output using cout.
This "Hello World" example helps you understand the basic structure of any C++ program:
Include libraries
Use namespace
Write main() function
Write your logic
End the program

Once you understand this simple C++ Hello World program, you are ready to learn more:

variables, data types, conditions, loops, and functions.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Hello World

This C++ program is part of the "Basic Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents