Basic Structure of C Program (Complete Tutorial)
In this video tutorial, you will see how to write the structure of C program. I will explain the entire structure of C program with all its components. It is part of FBISE class 10 Computer Science book. Fundamentals of C programming starts from chapter 2 of computer science book.
You can also download FBISE past papers for any class including SLO past papers and model papers for 9th, 10th, first year and second year.
Structure of C Program
Structure of C program is the following:
Preprocessor Directives
Main function (void)
{
Local declarations
Statements
}
user-defined Function
Function 1
Function 2
Function 3
Preprocessor Directives
Preprocessor directives are compiler instructions which perform some processing before executing the program. In C language there are two kinds of preprocessor directives i.e. #include and #define.
#include preprocessor directive is used to add header files in a C program. For example, some common header files are stdio.h, math.h and conio.h.
#define preprocessor directive is used to define a constant value in the program and it is written at the top of the program.
The main() Function
The main() function is the default function of C program. It executes first of all even more functions are added in a program. First void in main function shows that this function does not return any value. Second void inside the parentheses shows that main function does not take any argument. Then curly braces indicate the start and end of body of main function.
Local Declarations
Most programs use local variables for storing data in the body of main function. These variables need to be declared before using them in the program. Local variables can be used inside the block of main function only and cannot be accessed outside the function.
Statements
Program statements can be used inside the body of main function. These statements can be input/output statements or conditional statements. These statements execute as they appear in the program or skip statements if they are conditional ones.
Delimiters { }
The curly braces start after the first line of main function. They show the start and end of main function. They enclose the body of main function. These braces are called delimiters.
User-defined Function
It is optional part of structure of C program. We use several user-defined functions if a program is large in size then we divide it into different sections using functions. You can use it only when you add more functions in your program. In most of the cases, programs don’t use additional functions so this part of program is excluded. Therefore, you may skip it at this point.
Function 1
Function 1 shows your first additional function in the program which you can use for a specific purpose. Such as addition() function can be used to add two numbers.
Function 2
Function 2 represents your second additional function the program. You can use multiple functions for various purposes. Such as if you are creating a program of a calculator then separate functions can be used for addition, subtraction, multiplication, division, etc.
Function 3
Similarly you can add third function in the program if desired. You can use it for some other purpose. It is highly recommended to break a larger program into small divisions. Then it is easy to code and find issues in the program.
Conclusion
In the end, structure of C program is important and basic step in learning programming in C language. Preprocessor directives are used to add header files and define constant values. Main function is the default function of C program which executes first in any C program. The main function is followed by the body of main function which consists of local variables and program statements.
The structure of normal C programs that don’t use any additional functions will be ended here. After the main function additional functions can be defined for various purposes. Therefore, this section is used for larger programs when we need to break the program into smaller sections.
For sample textbook chapters visit the official site of national book foundation.