C++ Program Structure
A C++ program follows a specific structure. Understanding the structure helps programmers organize their code and write programs correctly.
Although C++ programs can vary in complexity, most programs contain the same basic components.
Basic Structure of a C++ Program
#include <iostream>
using namespace std;
// main function
int main() {
cout << "Hello, World!";
return 0;
}
Explanation of Each Part
1. Header Files
The #include statement is used to include libraries that provide additional functionality.
#include <iostream>
This library allows the program to perform input and output operations using cin and cout.
2. Namespace Declaration
using namespace std;
This line allows us to use standard library functions without writing std:: before them.
3. Main Function
int main()
The main() function is the starting point of every C++ program. Program execution begins from this function.
4. Program Statements
cout << "Hello, World!";
Statements inside the main() function perform actions such as displaying output or performing calculations.
5. Return Statement
return 0;
This statement ends the program and returns a value to the operating system. A return value of 0 usually indicates successful execution.
Important Notes
- Every C++ program must contain a main() function.
- Statements in C++ end with a semicolon (;).
- Code blocks are enclosed within curly braces { }.
- Comments can be added to explain the code.
Next Topic
Next, learn about C++ Syntax.