Structure of C++ Program Last Updated : 02 Nov, 2023 Comments Improve Suggest changes Like Article Like Report The C++ program is written using a specific template structure. The structure of the program written in C++ language is as follows: Documentation Section:This section comes first and is used to document the logic of the program that the programmer going to code.It can be also used to write for purpose of the program.Whatever written in the documentation section is the comment and is not compiled by the compiler.Documentation Section is optional since the program can execute without them. Below is the snippet of the same: C++ /* This is a C++ program to find the factorial of a number The basic requirement for writing this program is to have knowledge of loops To find the factorial of number iterate over range from number to one */ Linking Section:The linking section contains two parts: Header Files: Generally, a program includes various programming elements like built-in functions, classes, keywords, constants, operators, etc. that are already defined in the standard C++ library.In order to use such pre-defined elements in a program, an appropriate header must be included in the program.Standard headers are specified in a program through the preprocessor directive #include. In Figure, the iostream header is used. When the compiler processes the instruction #include<iostream>, it includes the contents of the stream in the program. This enables the programmer to use standard input, output, and error facilities that are provided only through the standard streams defined in <iostream>. These standard streams process data as a stream of characters, that is, data is read and displayed in a continuous flow. The standard streams defined in <iostream> are listed here.#include<iostream> Namespaces: A namespace permits grouping of various entities like classes, objects, functions, and various C++ tokens, etc. under a single name.Any user can create separate namespaces of its own and can use them in any other program.In the below snippets, namespace std contains declarations for cout, cin, endl, etc. statements.using namespace std;Namespaces can be accessed in multiple ways:using namespace std;using std :: cout;Definition Section:It is used to declare some constants and assign them some value.In this section, anyone can define your own datatype using primitive data types.In #define is a compiler directive which tells the compiler whenever the message is found to replace it with “Factorial\n”.typedef int INTEGER; this statement tells the compiler that whenever you will encounter INTEGER replace it by int and as you have declared INTEGER as datatype you cannot use it as an identifier.Global Declaration Section:Here, the variables and the class definitions which are going to be used in the program are declared to make them global.The scope of the variable declared in this section lasts until the entire program terminates.These variables are accessible within the user-defined functions also.Function Declaration Section:It contains all the functions which our main functions need.Usually, this section contains the User-defined functions.This part of the program can be written after the main function but for this, write the function prototype in this section for the function which for you are going to write code after the main function. C++ // Function to implement the // factorial of number num INTEGER factorial(INTEGER num) { // Iterate over the loop from // num to one for (INTEGER i = 1; i <= num; i++) { fact *= i; } // Return the factorial calculated return fact; } Main Function:The main function tells the compiler where to start the execution of the program. The execution of the program starts with the main function.All the statements that are to be executed are written in the main function.The compiler executes all the instructions which are written in the curly braces {} which encloses the body of the main function.Once all instructions from the main function are executed, control comes out of the main function and the program terminates and no further execution occur.Below is the program to illustrate this: C++ // Documentation Section /* This is a C++ program to find the factorial of a number The basic requirement for writing this program is to have knowledge of loops To find the factorial of a number iterate over the range from number to 1 */ // Linking Section #include <iostream> using namespace std; // Definition Section #define msg "FACTORIAL\n" typedef int INTEGER; // Global Declaration Section INTEGER num = 0, fact = 1, storeFactorial = 0; // Function Section INTEGER factorial(INTEGER num) { // Iterate over the loop from // num to one for (INTEGER i = 1; i <= num; i++) { fact *= i; } // Return the factorial return fact; } // Main Function INTEGER main() { // Given number Num INTEGER Num = 5; // Function Call storeFactorial = factorial(Num); cout << msg; // Print the factorial cout << Num << "! = " << storeFactorial << endl; return 0; } OutputFACTORIAL 5! = 120 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Structure of C++ Program B believer411 Follow Improve Article Tags : C++ Programs Articles C++ Practice Tags : CPP Similar Reads Tree C/C++ Programs Trees are hierarchical data structures that contain nodes connected by edges. They are recursive in nature, which means that they are made up of smaller instances of themselves. Various types such as binary tree, trie, etc. have different characteristics to make them suitable for different applicati 2 min read C/C++ Programs sArray C/C++ ProgramsC Program to find sum of elements in a given arrayC program to find largest element in an arrayRecursive C program to linearly search an element in a given arrayC program to multiply two matricesC/C++ Program for Given an array A[] and a number x, check for pair in A[] with sum 15+ min read Stack and Queue C/C++ Programs The stack and queue are popular linear data structures with a wide variety of applications. The stack follows LIFO (Last In First Out) principle where the data is inserted and extracted from the same side. On the other hand, the queue follows FIFO (First In First Out) principle, i.e., data is insert 3 min read Array of Pointers to Strings in C++ In C++, an array is a homogeneous collection of data that is stored in a contiguous memory location. We can store almost all types of data as array elements. In this article, we will learn how to store the array of pointers to strings in C++. Array of Pointers to Strings in C++A pointer to a string 6 min read Classes vs Structure vs Union in C++ Class: It is a user-defined datatype enclosed with variables and functions. It is like a blueprint for an object. Class members are private by default. For Example, the car is an object, its color, design, weight are its attributes whereas the brake, speed limit, etc. are its functions. Syntax: clas 4 min read Like