Structure of C program
C is a general-purpose, POP (Procedural Oriented Programming) language.
C is one of those language which has set structure for program which is called as structure programming.
Following is the basic structure for C program
- Documentation Section
- Link Section/Pre-processor Statements
- Global Declarations/Definition Section
- main() function
- Local Declarations
- Program statements and expression
- Function Definition
Documentation Section
This section consists of all the information regarding the program information like aim, author, date, etc. of program. This section is always commented out. This section is for inform the others regarding the code.
Link Section/Pre-processor Statements
To run program there are some header files which we will use in our program eg. stdio.h, conio.h, string.h, etc. those files contains the body or definition for many functions which we will be using in our program. For instance printf() and scanf() which we will use more often are defined in side “stdio.h” header file.
Link section is to specifies such header files. Compiler load all the header files in our program before it starts with our code.
In C we use “#include<’header file name’>” to include header file in program.
Global Declarations/Definition Section
In this section we will declare of define variables which are common for all the functions in the file. We can declare or define the user defined functions in this section. We can create macros in this section eg “#define PI 3.14” which means if we wrote “PI” in program that means it is “3.14” it’s not same as global variables because in case of global variable it can be updated (if it is not constant) but on other hand macro remains constant throughout the file.
Global variable has allocated memory unlike macro which does not but it gets processed faster.
This section is right before main() function.
main() function
When we compile or execute the program compiler searches for main(). main() is the starting point of any program. Even if we create our own functions but if we din’t call them in main() function the compiler is not going to check them. Compiler reads only those functions which are called in main().
We can declare or define variables here in main().
Local Declarations
Like name says the variables which are declared or defined in this main() can only be used by the main() function. They are not accessible to all the other functions.
Program statements and expression
In this section we will be writing our business logic. Not necessary to write the complete logic in main() and also it’s not a good practice. We have to modularize our logic in functions and call those functions in main().
Function Definition
We can define our own function in this section but we have to declare those function in global declaration section. It is good practice to define own function after main(). In c it’s compulsory to declare the function in global declaration section even though it is dependent on which compiler we are using but the traditional method says that we have to declare the function in global section.
Example:
/*****Documentation Section*****/ /* Author : ITVoyagers (https://itvoyagers.in/) Date : 21st November 2018 Description : Program to print "Hello!!!! ITVoyagers here" */ /*****Link Section/Pre-processor Statements*****/ #include<stdio.h> /*****main() function*****/ void main() { /*****Program statements and expression*****/ printf("n Hello!!!! ITVoyagers here"); }
Note: We know that above example is pretty basic and does not contain all the sections of C program but this is just an example or we can say that first step.
Little advance example:
/*****Documentation Section*****/ /* Author : ITVoyagers (https://itvoyagers.in/) Date : 21st November 2018 Description : Program to print "Hello!!!! ITVoyagers here" */ /*****Link Section/Pre-processor Statements*****/ #include<stdio.h> /*****Global Declarations/Definition Section*****/ int num1 = 2; int add(int var1, int var2); /*****main() function*****/ void main() { /*****Local Declarations*****/ int num2 = 2, num3; /*****Program statements and expression*****/ num3 = add(num1,num2); printf("n num1 + num2 = %d", num3); } /*****Function Definition*****/ int add(var1, var2) { return var1 + var2; }
Note: Okay!! this example is little more advance then previous one and it is okay if you don’t understand it. Our aim is to show the full Structure of C program. Trust us we will make it for you to read and write C program down the road.