Code Strut of C-Program
Code Strut of C-Program
The C++ program is written using a specific template structure. The structure of the program
written in C++ language is as follows:
Documentation
Link Section
Definition Section
Main Function
Skelton of C Program
● 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 is written in the documentation section is the comment and is not compiled
by the compiler.
● The documentation Section is optional since the program can execute without them.
Below is the snippet of the same:
Linking Section
The linking section contains two parts:
#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 bellow snippets, namespace std contains declarations for cout, cin, endl, etc.
statements.
Definition Section
● Here the variables and the class definitions which are going to be used in the
program are declared to make them global.
● 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.
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 furter execution occur.
// 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 k;
// Function Section
k factorial(k& num)
{
//iterate over the loop from num to one
for (k i=1; i<=num; i++)
{
fact *= i;
}
// Return the factorial
return fact;
}
// Main Function
int main()
{
//Given number Num
k Num = 5;
//Function Call
storeFactorial = factorial(Num);
cout << msg;
Return 0;
Output
FACTORIAL
5! = 120
1
Source: https://www.geeksforgeeks.org/structure-of-c-program/