My Blog
Articles related to programming, computer science, technology and research.

22/04/2016 Categories: C Programming. Comments Off on Structure of a C Program

Every program written in C follows a certain structure. That structure is as shown below:





Documentation Section


The documentation section is used to improve the readability and to understand various elements in the program. This section is used to provide help for the general users. This section consists of plain text written in English and includes information like author, purpose of the program, date on which the program was created etc. This section is included inside comments.




Link Section


The link section is used to include the external header files in which the pre-defined functions are available. To use the pre-defined functions in our C programs, these files must be linked with our program. Files can be included by using the “include” directive. Example:




In the above example “stdio.h” is a header file which is available in the system area in the programmer’s computer. The angular brackets “< and >” tells the compiler to search the file “stdio.h” in the system area.


If we specify the above example as #include”stdio.h”, then the compiler searches for the file “stdio.h” in the current directory.


Definition Section


This section is used to define symbolic constants. Symbolic constants can be declared by using the #define directive. Example:


#define PI 3.142


Global Declaration Section


This section is used for declaring global variables which can be throughout the program and for declaring function prototypes. The global variables can be accessed by any function within the file.


main() Function Section


The main() function is necessary in every C program. It specifies the starting point of execution in a program. There should be a main() declared in every C program. The main() function consists of the executable instructions and declaration statements. It may call other user defined functions.


Subprogram Section


This section includes the definitions of user-defined functions. A function is essentially a set of instructions for performing a task. We can divide a problem into sub problems and write a function for solving each sub problem.


Example: HelloWorld Program



In the above example we can see three sections: documentation section, link section and main() function section.


In the above program, /* and */ denotes multi-line comments. The /* indicates the starting of multi-line comment and the */ indicates the ending of multi-line comments. Everything included between /* and */ is ignored by the compiler.


In the next line after the multi-line comments, #include is a pre-processor directive which is used to link the program with external files like header files in which the predefined functions are declared.


The main() function is essential in every C program, as it specifies the starting point of execution in a program. Everything starting from { to } is known as the body of the main function.


clrscr() function is used to clear the output screen (console). The printf() function is used to output characters onto the console. The getch() function pauses the execution of the program, and waits for a single character input from the user.


The declaration of printf() is available in stdio.h (Standard Input Output) header file and the declarations of clrscr() and getch() are available in conio.h (Console Input Output) header file.



Related Links:

Scroll Up