C Program Structure Explanation

In this tutorial, we will discuss about c program structure. Any programming language has some basic structures. Every program in that language follows that structure. In c language, we also have a structure. Below image depicts the structure very well.

Tutorialwing  - C program structure

Tutorialwing – C program structure

Let’s see the section one by one…

Documentation section

This section mainly contains comments. In comments, we write name of the program, author, about company etc. You can also write the date on which the file was created. Basically, this contains a brief introduction of the file.

Link section

While writing program in c, we use several keywords and statements, library methods. For example, puts, scanf, printf etc. These functions, keywords, or statements are kept in a separate file. So, you must include link to these files in order to use it. We use #include directive to include any files. For example, #include stdio.h . We use “stdio.h” header file for reading the data from terminal and to display the data on terminal. There are several such header files. You will get error if you try to use library methods without including header files into your program.

Definition section

The definition section contains symbolic constants. By symbolic constant we mean some symbol will act as constant. We define such constants using #define directive. For example, #define TRUE 1. So, whenever we will write TRUE in the program, it would mean value 1.

Global section

Global section contains all the global variables. For example, int total_count = 4. You can access and manipulate this variable everywhere. This section also contains declaration of user defined functions.

Main function section

It is main section of any c program. Your program execution start from here. Any c program must have this main function.




Subprogram section

Subprogram section includes all the user defined function. These functions may be called from other user defined function or main function. If you have many user defined function, at-least one user defined will be called from main function.

Let’s take an example to clear the things more…..

//  Document section....
//  Basic program demonstrating structure of any c program
//  main.c
//  Created by Rajeev kumar on 01/04/17.
//  Copyright © 2017 Rajeev kumar. All rights reserved.
//

#include <stdio.h>   // Link section...

#define TRUE 1       // definition section...
void printHello();   // definition section...

// Main section starts here....
int main(int argc, const char * argv[]) {
    // insert code here...
    printf("Hello, World!\n");
    
    if(TRUE) {
        printf("Hello from definition section \n");
    }
    
    printHello();
    return 0;
}
// Main section ends here....

// sub program section starts here....
void printHello() {
    printf("Hello from sub program\n");
}
// sub program section ends here....

Conclusion

Here, we have discussed about c program structure. Every programming language follows some basic structure. C also has such structures. C program is divided into many sections. we have covered each and every section clearly. If you have any questions regarding this topic, please feel free to contact us.

Leave a Reply