Learn with Yasir

Share Your Feedback

Your First C++ Program: Hello World Explained


Write your first C++ program with a simple Hello, World! example. Learn what #include , main(), cout, and return 0 do, step by step.

1. What is C++?

C++ is a programming language used to create different types of software, applications, games, operating systems, and many other programs.

A C++ program is written as source code. The computer then converts this code into a form that the computer can understand and execute.


2. My First C++ Program

Let us start with a very simple program:

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello, World!";
    
    return 0;
}

Output

Hello, World!

This is usually the first program, write in C++.


3. Understanding the Program

Let’s understand each line step by step.


Line 1: #include <iostream>

#include <iostream>

What does it mean?

#include tells C++ to include a library in our program.

iostream stands for:

Input/Output Stream

It provides facilities for taking input from the user and displaying output on the screen.

For example, we need iostream to use:

cout

and

cin

Example

#include <iostream>

Without including iostream, we cannot normally use cout and cin.

Simple idea

Think of a library as a toolbox.

iostream = Toolbox
cout     = Tool for displaying output
cin      = Tool for taking input

4. What is #include?

#include

#include is called a preprocessor directive.

It tells the preprocessor to include the required library/header file before the actual compilation of the program.

Important

A preprocessor directive starts with:

#

Examples:

#include <iostream>
#define PI 3.14159

For beginners, remember:

#include is used to include a library/header file in a C++ program.


5. Line 2: using namespace std;

using namespace std;

Let’s break it down.

std

std means standard.

C++ provides many standard features inside the std namespace.

For example:

std::cout

is the standard output object.

Instead of writing:

std::cout << "Hello";

we can write:

cout << "Hello";

when we have:

using namespace std;

Explanation

Think of std as a folder containing standard C++ tools.

std
 ├── cout
 ├── cin
 ├── string
 └── endl

using namespace std; allows us to use these standard tools without repeatedly writing std::.


6. Line 3: int main()

int main()

This is one of the most important parts of a C++ program.

main() is the starting point of a C++ program.

When we run the program, execution starts from:

main()

What does int mean?

int means integer.

Here, int tells us that the main() function will return an integer value.

So:

int main()

means:

The main function returns an integer value.


7. What is main()?

main()

main() is a function.

A function is a block of code designed to perform a particular task.

The main() function is special because it is where the execution of a C++ program begins.

Simple example

Think of a program as a book.

C++ Program
     ↓
  main()
     ↓
Program execution starts here

8. What are { }?

Our program contains:

{
    cout << "Hello, World!";
    
    return 0;
}

The symbols:

{
}

are called curly braces or braces.

They mark the beginning and end of a block of code.

{

Opening brace — starts the block.

}

Closing brace — ends the block.

For example:

int main()
{
    // Program code
}

Everything between { and } belongs to the main() function.


9. cout << "Hello, World!";

cout << "Hello, World!";

This statement displays text on the screen.

cout

cout is used to display output.

Think:

cout = output

<<

The symbol:

<<

is called the insertion operator when used with cout.

It sends the data toward cout for display.

"Hello, World!"

This is the text that we want to display.

Complete statement

cout << "Hello, World!";

means:

Display Hello, World! on the screen.


10. What are " "?

In our program:

"Hello, World!"

the text is enclosed in double quotation marks.

" "

Double quotation marks are used to represent a string literal.

For example:

cout << "Welcome";

Output:

Welcome

Another example:

cout << "My name is Ali";

Output:

My name is Ali

11. Why is there a semicolon ;?

Notice:

cout << "Hello, World!";

There is a semicolon at the end.

;

A semicolon is generally used to indicate the end of a statement in C++.

For example:

int age = 20;
cout << age;
return 0;

Important

A common mistake is forgetting the semicolon (;) at the end of a statement.

Incorrect:

cout << "Hello"

Correct:

cout << "Hello";

12. return 0;

The last statement is:

return 0;

It returns the value 0 from the main() function.

Remember:

int main()

means main() returns an integer.

Therefore:

return 0;

returns an integer value of 0.

you can understand it as:

The program has finished successfully.


13. Complete Program Structure

A basic C++ program can look like this:

#include <iostream>

using namespace std;

int main()
{
    // Program statements

    return 0;
}

Let’s visualize it:

#include <iostream>
        ↓
Include required library

using namespace std;
        ↓
Use standard C++ features

int main()
        ↓
Program starts here

{
        ↓
Beginning of main function

Program statements
        ↓
Instructions executed by computer

return 0;
        ↓
End main function successfully

}
        ↓
End of program block

14. Modify Our First Program

Original program:

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello, World!";

    return 0;
}

Now change the message:

#include <iostream>

using namespace std;

int main()
{
    cout << "Welcome to C++ Programming!";

    return 0;
}

Output

Welcome to C++ Programming!

15. Display Your Name

Try this program:

#include <iostream>

using namespace std;

int main()
{
    cout << "My name is Muhammad.";

    return 0;
}

Output

My name is Muhammad.

Students can replace Muhammad with their own name.


16. Display Multiple Lines

We can use endl to move to the next line.

#include <iostream>

using namespace std;

int main()
{
    cout << "My name is Ali." << endl;
    cout << "I am a student." << endl;
    cout << "I am learning C++.";

    return 0;
}

Output

My name is Ali.
I am a student.
I am learning C++.

Understanding endl

endl

means end line.

It moves the cursor to the next line.

For example:

cout << "Hello" << endl;
cout << "Welcome";

Output:

Hello
Welcome

17. Another Way to Print Multiple Lines

We can also use \n.

#include <iostream>

using namespace std;

int main()
{
    cout << "My name is Ali.\n";
    cout << "I am a student.\n";
    cout << "I am learning C++.";

    return 0;
}

Output:

My name is Ali.
I am a student.
I am learning C++.

\n means new line.


18. Important C++ Terms

Term Meaning
C++ A programming language
Program A set of instructions
#include Includes a header/library
iostream Input/output library
main() Starting point of the program
int Integer data type
cout Displays output
<< Sends data to cout
{ } Defines a block of code
; Ends a statement
return 0 Returns 0 from main()
endl Moves output to next line
\n New-line character

19. Task

Task

Write a C++ program that displays:

Welcome to C++ Programming
My name is __________
I am a student
I am learning programming

Solution

#include <iostream>

using namespace std;

int main()
{
    cout << "Welcome to C++ Programming" << endl;
    cout << "My name is Muhammad" << endl;
    cout << "I am a student" << endl;
    cout << "I am learning programming";

    return 0;
}

replace Muhammad with their own name.


20. Remember These 5 Things

For your first C++ program, remember:

  1. #include <iostream> → allows us to use input/output features.
  2. using namespace std; → allows us to use standard features such as cout without writing std::.
  3. int main() → execution starts here.
  4. cout → displays output.
  5. ; → marks the end of a statement.

First Program to Memorize

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello, World!";

    return 0;
}

Best approach: first understand what each line does, then modify the message, and finally write the program yourself without looking at the example.