Structure of a C++ Program
In this lesson: Basic structure of a C++ program — headers, namespace, main, statements — labelled example with output. In Hindi & English.
What does "structure" mean?
Every C++ program, from a two-line "Hello" to a school management system, is built from the same parts arranged in the same order. Knowing this structure helps you read any program quickly and write your own without forgetting a piece. It is also a favourite short-answer question in Class 11–12 and first-year exams.
The parts at a glance
| # | Part | Example | Required? |
|---|---|---|---|
| 1 | Documentation (comments) | // Program to find average marks | No, but recommended |
| 2 | Preprocessor directives | #include <iostream> | Yes, if you use library features |
| 3 | Namespace declaration | using namespace std; | No |
| 4 | Global declarations | const int SUBJECTS = 5;, structs, classes | No |
| 5 | Function declarations / definitions | double average(int total); | No |
| 6 | main() function | int main() { ... } | Yes — exactly one |
A complete example
// 1. Documentation: program to calculate average marks of a student
// 2. Preprocessor directive
#include <iostream>
// 3. Namespace
using namespace std;
// 4. Global declaration
const int SUBJECTS = 5;
// 5. Function declaration (prototype)
double average(int total);
// 6. main() — execution starts here
int main() {
int marks[SUBJECTS] = {78, 85, 69, 92, 74};
int total = 0;
for (int i = 0; i < SUBJECTS; i++) {
total += marks[i];
}
cout << "Total : " << total << endl;
cout << "Average : " << average(total) << endl;
return 0;
}
// 5. Function definition
double average(int total) {
return static_cast<double>(total) / SUBJECTS;
}Each part explained
1. Documentation section
Comments at the top describe the program's purpose, author and date. The compiler ignores them. See comments in C++.
2. Preprocessor directives
Lines starting with # are handled by the preprocessor before compilation. #include <iostream> copies in the declarations for cin and cout. Other common headers are <string>, <vector>, <cmath> and <iomanip>. See header files.
3. Namespace declaration
The standard library lives in the namespace std. using namespace std; lets you write cout instead of std::cout. It is convenient in small programs; in large projects write std:: explicitly. See namespaces.
4. Global declarations
Constants, structures, classes and (rarely) variables declared outside every function are global — visible to all functions below them. Keep global variables to a minimum; global constants are fine.
5. Functions
A function must be declared before it is called. You can either define it above main(), or put a prototype (double average(int total);) above main() and the full definition below — as in the example.
6. The main() function
int main() is where the program begins. Its body is enclosed in { }. return 0; tells the operating system the program ended successfully. There must be exactly one main() in a program.
How the program is processed
- Preprocessing: comments are removed and
#includefiles are inserted. - Compilation: the code is checked for errors and turned into object code.
- Linking: your object code is joined with library code (for example, the code behind
cout) to make an executable. - Execution: the operating system starts the program at
main().
g++ average.cpp -o average ./average
Statements, blocks and semicolons
- Every statement ends with a semicolon
;. - A group of statements inside
{ }is a block; function bodies, loops andifbodies are blocks. - C++ is case-sensitive:
Main,MAINandmainare different names. - Whitespace and line breaks are free-form, but good indentation matters to readers.
Structure with a class
In object-oriented programs, a class is usually declared in the global section and used from main():
#include <iostream>
#include <string>
class Student {
public:
std::string name;
int marks;
void show() const {
std::cout << name << " scored " << marks << "\n";
}
};
int main() {
Student s{"Priya", 91};
s.show();
return 0;
}Common mistakes
- Missing
#include <iostream>— "coutwas not declared in this scope". - Calling a function before it is declared and without a prototype.
- Writing
void main()— standard C++ requiresint main(). - Forgetting the semicolon after a class or struct definition:
class A { }; - Two
main()functions in files that are compiled together.
Write a program with all six parts: a header comment, #include <iostream>, a global constant TAX_RATE, a function prototype double withTax(double price), main() that reads a price and prints the price with tax, and the function definition after main().
Summary
- A C++ program has documentation, preprocessor directives, namespace, global declarations, functions and
main(). - Only
main()is compulsory, and there must be exactly one. #includelines are processed before compilation.- Declare (or prototype) functions before calling them.
return 0;signals successful completion.
Next: comments in C++ · Back: first C++ program.
Frequently Asked Questions
What is the basic structure of a C++ program?
#include <iostream>, (2) an optional using declaration or namespace, (3) global declarations like constants, structures and classes, (4) user-defined functions, and (5) the main() function where execution starts, usually ending with return 0;.Why is main() necessary in C++?
main() is the entry point: when the operating system runs your program, execution begins at the first statement of main(). A complete program must have exactly one main() function, otherwise the linker reports an "undefined reference to main" error.What does return 0 mean in C++?
return 0; ends main() and sends the value 0 back to the operating system, which by convention means "the program finished successfully". A non-zero value signals an error. If you leave it out of main(), C++ automatically returns 0.