In this article we will learn about the structure of a C program. This article is a part of C programming tutorial for beginners.
Every program written in C programming language follows a certain structure. That structure is as shown below:
Watch the below video to learn about structure of a C program:
Contents
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.
Example:
/*********************************
* Author: Name
* Date: 7/10/2012
* Purpose: Theme of the program
********************************/
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:
#include<stdio.h>
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
A C program for displaying the text “HelloWorld” is given below:
/*********************************
* Author: Teja
* Date: 7/10/2012
* Purpose: To print Hello World
********************************/
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf(“HelloWorld!”);
getch();
}
Watch the below video for hello world program using C language:
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.
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply