Structure of C Program

quiz
Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Overview

Most programming languages have a structure, including the C language. A C program is divided into six sections: Documentation, Link, Definition, Global Declaration, Main() Function, and Subprograms. While the main section is compulsory, the rest are optional in the structure of the C program.

Scope of Article

  • We'll look after the structure of the C program.
  • Different sections in the structure of the C program.
  • Examples to understand better.

Introduction to Structure of the C Program

All human beings have a definite structure, i.e., head, neck, and four limbs connected to a torso. Almost everything has a definite structure. Likewise, in the case of programming languages, all of them have a definite structure. These structures have to be followed while writing the code.

The structure of a C program can be mainly divided into six parts, each having its purpose. It makes the program easy to read, easy to modify, easy to document, and makes it consistent in format.

Basic Structure of the C Program

SectionDescription
DocumentationConsists of the description of the program, programmer's name, and creation date. These are generally written in the form of comments.
LinkAll header files are included in this section which contains different functions from the libraries. A copy of these header files is inserted into your code before compilation.
DefinitionIncludes preprocessor directive, which contains symbolic constants. E.g.: #define allows us to use constants in our code. It replaces all the constants with its value in the code.
Global DeclarationIncludes declaration of global variables, function declarations, static global variables, and functions.
Main() FunctionFor every C program, the execution starts from the main() function. It is mandatory to include a main() function in every C program.
SubprogramsIncludes all user-defined functions (functions the user provides). They can contain the inbuilt functions and the function definitions declared in the Global Declaration section. These are called in the main() function.

Let's look at an example to understand the structure of a C program:

Example: Write a program to calculate our age.

In the following example, we'll calculate age concerning a year.

Algorithm

You've to subtract the current year from your birth year and get your age.

Let's implement this and check:

Code:

/**                     //Documentation
 * file: age.c
 * author: you
 * description: program to find our age.
 */

#include <stdio.h>      //Link

#define BORN 2000       //Definition

int age(int current);   //Global Declaration

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

int age(int current) {     //Subprograms
    return current - BORN;
}

Output

Age: 21

Let's explore the code:

Different sections of the above code

Documentation

In a C program, single-line comments can be written using two forward slashes i.e., //, and we can create multi-line comments using /* */. Here, we've used multi-line comments.

/**
 * file: age.c
 * author: you
 * description: program to find our age.
 */

All header files are included in this section.

A header file is a file that consists of C declarations that can be used between different files. It helps us in using others' code in our files. A copy of these header files is inserted into your code before compilation.

#include <stdio.h>

Definition

A preprocessor directive in C is any statement that begins with the "#" symbol. The #define is a preprocessor compiler directive used to create constants. In simple terms, #define basically allows the macro definition, which allows the use of constants in our code.

#define BORN 2000

We've created a constant BORN which is assigned a value of 2000. Generally, uppercase letters are preferred for defining the constants. The above constant BORN will be replaced by 2000 throughout our code wherever used.

#define is typically used to make a source program easy to modify and compile in different execution environments.

The define statement does not ends with a semicolon.

Global Declaration

This section includes all global variables, function declarations, and static variables. The variables declared in this section can be used anywhere in the program. They're accessible to all the functions of the program. Hence, they are called global variables.

int age(int current);

We've declared our age function, which takes one integer argument and returns an integer.

Main() Function

In the structure of a C program, this section contains the main function of the code. The C compiler starts execution from the main() function. It can use global variables, static variables, inbuilt functions, and user-defined functions. The return type of the main() function can be void and also not necessarily int.

int main(void)
{
  int current = 2021;
  printf("Age: %d", age(current));
  return 0;
}

Here, we've declared a variable named current and assigned the value as 2021. Then we've called the printf() function, with calls the age() function, which takes only one parameter.

Subprograms

This includes the user-defined functions called in the main() function. User-defined functions are generally written after the main() function irrespective of their order.

When the user-defined function is called from the main() function, the control of the program shifts to the called function, and when it encounters a return statement, it returns to the main() function. In this case, we've defined the age() function, which takes one parameter, i.e., the current year.

int age(int current) {
    return current - BORN;
}

This function is called in the main function. It returns an integer to the main function.

Conclusion

  • To conclude, the structure of a C program can be divided into six sections, namely - Documentation, Link, Definition, Global Declaration, Main() Function, and Subprograms.
  • The main() function is compulsory to include in every C program, whereas the rest are optional.
  • A well-structured C program makes debugging easier and increases the readability and modularity of the code.
Free Courses by top Scaler instructors
certificate icon
Certificates
C Tutorial
This program includes modules that cover the basics to advance constructs of C Tutorial. The highly interactive and curated modules are designed to help you become a master of this language.'
If you’re a learning enthusiast, this is for you.
Module Certificate
Criteria
Upon successful completion of all the modules in the hub, you will be eligible for a certificate.
You need to sign in, in the beginning, to track your progress and get your certificate.