C Program Structure
C Program Structure
1. Documentation,
2. Link,
3. Definition,
4. Global Declaration,
5. Main() Function and
6. Subprograms.
While the main section is compulsory, the rest are optional in the structure of the C
program.
The preprocessor directive is used to include any necessary header files and is used to
specify any constants that will be used in the program.
The main function is the main entry point of the program and it is responsible for calling
other functions.
Other functions are used to perform specific tasks within the program and they are usually
called from the main function.
The program termination section is used to clean up and release any resources used by
the program.
By understanding the structure of a C program, you can write more efficient and robust
programs. It is important to understand the structure of a C program before writing any code.
Basic Structure of the C Program
Section Description
Consists of the description of the program such as programmer's name, and
creation date. These are generally written in the form of comments.
e.g.: /*description section*/
Documentation or
//description line 1
//description line 2
All header files are included in this section which contains different functions
from the libraries. A copy of these header files is inserted into your code before
Link compilation.
e.g.: #include<stdio.h>; #include<stdlib.h> etc.
Includes pre-processor directive, which contains symbolic constants.
Definition e.g.: #define allows us to use constants in our code. It replaces all the constants
with its value in the code.
Global Includes declaration of global variables, function declarations, static global
Declaration variables, and functions.
For every C program, the execution starts from the main() function. It is
Main() Function mandatory to include a main() function in every C program.
Includes all user-defined functions (functions the user provides). They can
Subprograms contain the inbuilt functions and the function definitions declared in the Global
Declaration section. These are called in the main() function.
Algorithm
You've to subtract the current year from your birth year and get your age.
Code:
/** //Documentation
* file: age.c
* author: you
* description: program to find our age.
*/
Link: All header files are included in this section. A header file consists of C declarations. It
helps the programmer to use pre-defined functions that are already included in the C
compiler or it can be used to include any user made program to use the program’s functions
in the present program.
Definition: This section includes pre-processor directives, which contain symbolic constants
that can be used in the main() function or any sub-function of the program code.
Global Declaration: This section includes all global variables, function declarations, and
static variables. The variables declared in this section can be used anywhere in the program.
They're accessible to all the functions of the program. Hence, they are called global
variables.
Main() Function: In the structure of any C program, this section contains the main function
of the program code. The compiler starts executing from this main() function. The main()
function can use global variables, static variables, inbuilt functions, and user-defined
functions. The return type of the main() function can be void also.
Subprograms: This section consists of user defined functions that can be called from the
main() function. Whenever an user defined function is encountered in the main() function,
the control of the program get shifted to the sub-program and when a return statement is
fetched, the control re-returns to the main() function.