Structure of C Program with Example

In this tutorial, we will discuss what is the basic structure of C language? A C programming language is consist of 6 parts, which are (1) Documentation, (2) Link, (3) Definition, (4) Global Declaration, (5) Main() Function, and (6) Subprograms.

In addition, the main section is the only required while the rest are optional in C program structure. Let’s have a look at the most basic C programming structure so we may use it as a guide in the following tutorials.

About C Programming Structures

Each human has a definite structure. Practically everything has specific structures. Furthermore, in specific situations all programming languages have also definite structures.

Also, the basic structure of C program is composed of 6 parts and one of each has a purpose. The program is simple to read, to change, and to analyze, thus it has consistent formatting.

Basic Structure of C Program

The table below shows the basic structure of C programming language.

DocumentationIt contains a program description, the name of the creator of a program and the date of creating the code. Most of these are written for comments.
LinkAll of the header files are included in this section. It consists of various functions from the libraries. A copy of these header files is added to your code programs before a selection of compilation.
DefinitionIn definition it contains the following which are the preprocessor directive, and symbolic constants, for example #define allows you to use a constant variable in your code programs. Its value is substituted for all constants in the code.
Global DeclarationIts consists of declaration of global variables, declarations of functions and the static global variables function.
Main() FunctionIn C coding, the execution will begin in the main() function. It’s compulsory to include the main() function in all C program codes.
SubprogramsIt consists of all the user-defined functions and inbuilt functions. The main function() is a declaration of the global variable functions.

What is simple structure in C?

In C programming language, the structure is a set of variables or it can be various types by just one name.

Here is an illustration of a simple structure in C program.

Structure-of-C-Program
Structure-of-C-Program

Let’s have a look at the example below to figure out the structure of C program.

Example in structure of C program

We will create a program to calculate the age.

/**                     //For the Documentation
 * file: calcualte_age.c
 * author: sourcecodehero
 * description: a program to calculate the age.
 */

#include <stdio.h>      //For the Link

#define BORNYEAR 1992       //For the Definition

int source_age(int present);   //For the Global Declaration

int main(void)          //For the Main() Function
{
  int present = 2022;
  printf("Age: %d", source_age(present));
  return 0;
}

int source_age(int present) {     //For the Subprograms
    return present - BORNYEAR;
}

Output:

Age: 30

Next, We will analyze the code example.

Different Parts in Structure in C

Documentation

For the documentation in a C programming language, there are two types of comments documentation which are single line comments and multi line comments.

The single line comments are consist of two forward slashes, the symbol is “//” while multi line comments are consist of a symbol like this “/* */“.

In the code below we’ve used the multi line comments symbol.

/**                     //For the Documentation
 * file: calcualte_age.c
 * author: sourcecodehero
 * description: a program to calculate the age.
 */

All of the header files are included in this section. It consists of various functions from the libraries. A copy of these header file is added to your code programs before a selection of compilation.

#include <stdio.h>      //For the Link

Definition or Preprocessor Directive

For the definition/preprocessor directive in C language every statement starts with the symbol “#”. The #define allows you to use a constant variable in your code programs.

#define BORN 1992

We’ve declared the constant BORNYEAR, and we’ve given it the value 1992. In general, it is better to define the constants using uppercase characters. Every time the specified constant BORNYEAR is used in our code.

Furthermore, the #define is generally used to create a source program that is easy to change and compile in numerous executions.

In addition, the define functions there is no semicolon at the end.

Global Declaration

It consists of declaration of global variables, declarations of functions and static variables function. A declared variable in this area can be used all over in the program. It will be able to access all functions in the program. So that’s why it is called global variables.

int source_age(int present);   //For the Global Declaration

We declared the source_age functions, it will take one integer as an argument and it will return as an integer.

Main() Function

In this part of the structure of C Program consists the main statement of the code. Initially, the main() function is where the compiler begins to execute. It will be able to use the variables such as global, static, and inbuilt functions and the last are user-defined functions.

int main(void)          //For the Main() Function
{
  int present = 2022;
  printf("Age: %d", source_age(present));
  return 0;
}

In the code above we declared the variable name present then we will assign the value as 2022. Following the printf() functions and the age functions which is accepts only one parameter.

Subprograms

It consists of all the user-defined functions and inbuilt functions. The main function() is a declaration of the global variable functions.

However, if the user-defined function is being called into the main() function, as soon as the control of a program it will transfer to the called function.

Plus, in the code below we defined the source_age, which is it takes only one parameter and for example the present year.

int source_age(int present) {     //For the Subprograms
    return present - BORNYEAR;

Conclusion

To conclude, a C programming language is consist of 6 parts, which are (1) Documentation, (2) Link, (3) Definition, (4) Global Declaration, (5) Main() Function, and (6) Subprograms. The program is simple to read, to change, and to analyze, and it has consistent formatting.

In the previous tutorial, we discuss the C Environment Setup in Linux, Mac OS, and Windows and for the next tutorial, we will discuss the “C Basic Syntax.

Leave a Comment