Basic Structure of C Program with an Example

basic structure of a C program for example is a collection of variables of various data kinds identified by a single name.

Let’s examine an example to comprehend the necessity of structures in C programming.

Suppose we need to store student information such as names, ages, addresses, IDs, etc.

Introduction to Structure of C Program

This Introduction to the Structure of C Programs all humans have a distinct structure, consisting of a head, neck, and four limbs attached to a torso.

Virtually everything has a distinct structure. Similarly, each programming language has its own distinct structure.

These structures must be adhered to when writing code.

The structure of a C program can be divided into six major sections, each with its own function.

It makes the software easy to read, simple to alter, simple to document, and format consistently.

What is the basic structure of the C program?

DocumentationIt consists of the software description, the programmer’s name, and the date of creation. Typically, these are written in the form of comments.
LinkAll header files are included in this part, which contains various library functions. Before compilation, a copy of these header files is injected into your code.
DefinitionContains a preprocessor directive with symbolic constants. #define, for example, permits us to use constants in our code. It replaces each constant in the code with its value.
Global DeclarationIncludes global variable declarationsfunction declarations, static global variable declarations, and functions.
Main() FunctionEvery C program begins execution with the main() function. Every C program must contain the necessary main() function.
SubprogramsIt contains every user-defined function (functions the user provides). They may include both the built-in functions and function definitions defined in the Global Declaration section. These are invoked in the method main().

Before we learn the basic building elements of the C programming language, let us look at a bare minimum C program structure so that we may use it as a reference in the next chapters.

Consider the following example to comprehend the structure of a C program:

Example: Write software to calculate our age

In the example that follows, we will calculate age in terms of a year.


Your age is calculated by subtracting the current year from your birth year.

Let’s implement and verify:


/**                     //Documentation
 * file: calculate_age.c
 * author: itsourcecode
 * description: The program to find/calculate our age.

#include <stdio.h>      //Link

#define BORNED 1992  //Definition

int age(int current);   //Global Declaration

int main(void)          //Main() Function
  int current = 2022;
  printf("Age: %d", age(current));
  return 0;

int age(int current) {     //Subprograms
    return current - BORNED;


Age: 30

You can test the above example here!C Online Compiler

Let’s explain the code:

Various provisions of the above code


In a C program, single-line comments are written with two forward slashes, or //, and multi-line comments are typed with /* */.

We’ve used multiline comments here.

/**                     //Documentation
 * file: calculate_age.c
 * author: itsourcecode
 * description: The program to find/calculate our age.

All header files are contained in this part. A header file is a file containing C declarations that can be used across several files.

It enables us to include the code of others in our files. Before compilation, a copy of these header files is injected into your code.

#include <stdio.h>


In C, a preprocessor directive is any statement that begins with “#.”

The #define preprocessor directive is used to declare constants.

To put it simply, #define lets us define macros, which lets us use constants in our code.

#define BORNED 1992

The constant BORNED has been established and assigned the value 2022. Generally, capitalization is recommended when specifying constants.

The constant BORNED will be changed to 2022 everywhere it appears in our code. 

Typically, #define is used to make source code easier to alter and compile in various execution environments.

The definition clause does not conclude with a semicolon.

Global Declaration

This section contains all global variables, declarations of functions, and static variables. 

The variables stated in this section are accessible throughout the program.

They are accessible to all of the program’s operations. This section contains all global variables, declarations of functions, and static variables.

The variables stated in this section are accessible throughout the program.

They are accessible to all of the program’s operations. Consequently, they are known as global variables.

int age(int current);

We’ve declared the age function, which accepts a single integer parameter and returns an integer.

Main() Function

This section of a C program’s structure contains the code’s primary function.

The compiler executes the main() function first. It is capable of utilizing global variables, static variables, built-in functions, and user-defined functions.

The return type of the main() method may also be void and is not required to be an integer.

int main(void)          //Main() Function
  int current = 2022;
  printf("Age: %d", age(current));
  return 0;

Here, the variable current has been declared and assigned the value 2022.

Then, we invoked the printf() function, which in turn invoked the age() function, which accepts a single parameter.


This contains the user-defined routines called by main(). Regardless of their order, user-defined functions are typically written after the main() function.

When a user-defined function is called from the main() function, control passes to the called function, and when a return statement is encountered, control is returned to the main() function.

In this instance, we have defined the age() function, which accepts a single parameter, the current year.

int age(int current) {     //Subprograms
    return current - BORNED;

The main function invokes this function. The function returns a positive integer to the main function.


In summary, there are six main parts that make up the structure of a C program: documentation; links; definitions; global declarations; main() functions; and subprograms.

Every C program must contain the main() function, but the others are optional.

Debugging is made simpler, and the readability and modularity of the code are both improved by a well-structured C program.

Leave a Comment