In this lesson, we will create our first C++ program and understand each part of the program step by step. A basic C++ program contains a main function, statements, and, when required, header files for standard library features.
Here is a simple C++ program:
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
This program displays Hello World! on the screen.
First, create a new file and save it with the .cpp extension.
For example:
first.cpp
The .cpp extension is commonly used for C++ source files.
Our program uses std::cout to display output. The declaration for standard stream functionality is provided through the iostream header.
#include <iostream>
The #include directive makes the declarations from the specified header available to the program.
#include is a preprocessor directive. It tells the preprocessing stage to include the contents or declarations provided by a header file.
#include <iostream>
It is commonly written before the main function when the program needs features provided by that header.
iostream is a standard C++ header that provides facilities for standard input and output streams.
It is commonly used with:
The main() function is the entry point of a normal C++ program. Program execution begins from main.
int main() {
// Program statements
}
A C++ program normally needs a main function as its starting point.
In the declaration int main(), int is the return type of the main function.
int main() {
return 0;
}
The integer return value communicates the program's termination status to the environment.
The braces { } define the body of a function or other block of C++ code.
int main() {
// Code goes here
}
Statements written between these braces belong to the body of main.
std::cout is commonly used to send output to the standard output stream.
std::cout << "Hello World!";
The text inside the quotation marks is displayed as output.
The std:: prefix identifies a name that belongs to the standard namespace.
std::cout
std::cin
std::string
Using std:: explicitly makes it clear that the name comes from the C++ standard library namespace.
The insertion operator << is commonly used with std::cout to send values to the output stream.
std::cout << "Hello";
It can also be used several times in the same statement.
std::cout << "Hello " << "World!";
Text enclosed in double quotation marks is called a string literal.
"Hello World!"
For example:
std::cout << "Welcome to C++";
Many C++ statements end with a semicolon ;.
std::cout << "Hello";
The semicolon marks the end of the statement.
The statement return 0; returns the integer value 0 from the main function.
int main() {
std::cout << "Hello";
return 0;
}
A return value of zero is commonly used to indicate successful termination.
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
The program contains a header inclusion, the main function, an output statement, and a return statement.
You can use \n to move the output to the next line.
#include <iostream>
int main() {
std::cout << "Hello\n";
std::cout << "Welcome to C++\n";
std::cout << "Programming is fun!";
return 0;
}
std::endl inserts a newline into the output stream and also flushes the stream.
#include <iostream>
int main() {
std::cout << "Hello" << std::endl;
std::cout << "C++";
return 0;
}
C++ can also print numeric values using std::cout.
#include <iostream>
int main() {
std::cout << 100;
return 0;
}
The output will be:
100
You can display the value stored in a variable.
#include <iostream>
int main() {
int age = 20;
std::cout << age;
return 0;
}
The value stored in age will be displayed.
#include <iostream>
int main() {
int age = 20;
std::cout << "Age: " << age;
return 0;
}
Multiple values can be sent to std::cout using multiple << operators.
C++ can also take input from the user using std::cin.
#include <iostream>
int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Your age is: " << age;
return 0;
}
The extraction operator >> is commonly used with std::cin to read input.
int number;
std::cin >> number;
The entered value is stored in the variable number.
#include <iostream>
int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;
std::cout << "You entered: " << number;
return 0;
}
This program asks the user for a number and then displays the entered value.
We can use variables and arithmetic operators in our first programs.
#include <iostream>
int main() {
int a, b;
std::cout << "Enter two numbers: ";
std::cin >> a >> b;
int sum = a + b;
std::cout << "Sum = " << sum;
return 0;
}
Comments can be used to explain parts of a C++ program.
#include <iostream>
int main() {
// Display a message
std::cout << "Hello World!";
return 0;
}
The compiler does not treat the comment as an executable statement.
If you are using GCC, save the program as first.cpp and compile it with:
g++ first.cpp -o first
If there are no compilation errors, an executable named first (or first.exe on Windows) will be created according to the platform.
On Linux or macOS, the executable can commonly be run with:
./first
On Windows, depending on the terminal, you can commonly use:
.\first.exe
The program will then display its output.
Beginners commonly make these mistakes:
| Part | Purpose |
|---|---|
| #include <iostream> | Provides standard input/output stream facilities |
| int main() | Starting point of the program |
| { } | Defines the function body |
| std::cout | Displays output |
| << | Sends data to the output stream |
| return 0; | Returns a successful status from main |
Here is a complete beginner-friendly C++ program that uses input, processing, and output:
#include <iostream>
int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "You are " << age << " years old.";
return 0;
}
This program demonstrates the basic structure of a C++ application: including a header, starting execution with main(), taking input, and displaying output.
Question: Which function is the normal entry point of a C++ program?