Write your first C++ program with a simple Hello, World! example. Learn what #include
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.
Let us start with a very simple program:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
Hello, World!
This is usually the first program, write in C++.
Let’s understand each line step by step.
#include <iostream>#include <iostream>
#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
#include <iostream>
Without including iostream, we cannot normally use cout and cin.
Think of a library as a toolbox.
iostream = Toolbox
cout = Tool for displaying output
cin = Tool for taking input
#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.
A preprocessor directive starts with:
#
Examples:
#include <iostream>
#define PI 3.14159
For beginners, remember:
#includeis used to include a library/header file in a C++ program.
using namespace std;using namespace std;
Let’s break it down.
stdstd 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;
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::.
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()
int mean?int means integer.
Here, int tells us that the main() function will return an integer value.
So:
int main()
means:
The
mainfunction returns an integer value.
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.
Think of a program as a book.
C++ Program
↓
main()
↓
Program execution starts here
{ }?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.
cout << "Hello, World!";cout << "Hello, World!";
This statement displays text on the screen.
coutcout 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.
cout << "Hello, World!";
means:
Display
Hello, World!on the screen.
" "?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
;?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;
A common mistake is forgetting the semicolon (;) at the end of a statement.
Incorrect:
cout << "Hello"
Correct:
cout << "Hello";
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.
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
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;
}
Welcome to C++ Programming!
Try this program:
#include <iostream>
using namespace std;
int main()
{
cout << "My name is Muhammad.";
return 0;
}
My name is Muhammad.
Students can replace Muhammad with their own name.
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;
}
My name is Ali.
I am a student.
I am learning C++.
endlendl
means end line.
It moves the cursor to the next line.
For example:
cout << "Hello" << endl;
cout << "Welcome";
Output:
Hello
Welcome
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.
| 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 |
Write a C++ program that displays:
Welcome to C++ Programming
My name is __________
I am a student
I am learning programming
#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.
For your first C++ program, remember:
#include <iostream> → allows us to use input/output features.using namespace std; → allows us to use standard features such as cout without writing std::.int main() → execution starts here.cout → displays output.; → marks the end of a statement.#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.