In this tutorial you will learn about the C Program Structure and its application with practical example.
Like any other programming language, c language have specification available for c program structure, every c program is consist of the following building blocks –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Document Section //Optional Links Section (File) //Optioanal Definition Section //Optional Global variable declaration Section //Optional void main() { Local variable declaration Function declaration section Program statements; } Function definition 1 --------------------- --------------------- Function definition n |
- Documentations block
- Preprocessor Statements – Link Preprocessors, Definition Preprocessors
- Global declarations
- The main ( ) function
- Local Declarations
- Program statements
- User-defined functions
Documentations Block
Documentation block is the header of any c program structure which mainly consists of a set of comments, that is used to provide the information about the program written like name of a program, the utility of the program, date of creation, date of last modification, author name, licensing or copyrights information and any other information that programmer wish to put for the references.
Example:
1 2 3 4 5 6 7 |
//Program Name: First Program /* Version: 1.0 Description: C program basic program structure. Author: @noname Date Created:21-02-2013 */ |
Preprocessor Statements
This is the section where we define all the preprocessor directives, preprocessor statement begins with the # symbol. Preprocessor statements tell the compiler to include C preprocessors directives such as header files and constants prior to compilation of the C program. Preprocessor statements are further categorized as below –
Link Preprocessor- These preprocessor directives tell the compiler to link the functions from a system library.
Example:
1 2 |
#include <stdio.h> #include <conio.h> |
Definition Preprocessor – This is the section where we define all the symbolic constants we gonna use in our program.
Example:
1 2 3 |
#define pi 3.142 #define TRUE 1 #define FALSE 0 |
Global Declarations
This is the section where all the global variable and function declaration comes. All the variables and functions defined or declared outside the main function are treated as global.
The main ( ) function
This is the most vital part of each and every C program, it is mandatory for the C program to have only one main ( ). The C program execution starts with the main ( ) function. The c program can not be executed without the main function. It is illegal to terminate it by a semicolon. The main ( ) is the building block where all the logic of the program comes into the picture. The main ( ) is responsible for the execution of all the user-defined statements, functions, and library functions. The main ( ) function further structured into – Local variable declaration, function declaration and user-defined executable statements.
Local Declarations
In this section, we declare all the variables that will be used in the main ( ) function. Here we can also declare arrays, functions, pointers, etc. These variables can also be initialized with basic data types and initial values. For examples.
1 2 3 4 |
main ( ) { int sum = 0; int a; float b; } |
- All the local variable declaration is done in the variable declaration section.
- the main ( ) function can further have the declaration for user-defined functions.
- All the user-defined function definition appears immediately after the main function.
Program statements
This is the section where we place our main logic of the program which included the executable statements, that tell the computer to perform a specification action. Program statements can be input-output statements, arithmetic statements, control statements, simple assignment statements, and any other statements and it also includes comments that are enclosed within /* and */. The comment statements are ignored during the compilation and execution of the program and each executable statement must be ended with a semicolon.
- the main ( ) function should contain at least one executable statement.
- All statements in the declaration and executable parts end with a semicolon.
User-defined functions
This is the section of the C Program where we put all the user-defined sub-program or custom functions created to perform a specific task. A user-defined function must be defined before use it. A user-defined function can be written before or immediately after the main ( ) function and called inside the main ( ) function.