Structure of C Program with Example

Each and every human has a distinct structure, including a body, head, neck, and four limbs. The majority of things have a clear structure. Similarly to this, every programming language has a distinct structure. When developing the code, you must adhere to these structures.
A C program’s structure may be roughly broken down into six sections, each serving a specific function. It makes the program simple to understand, alter, and document, and makes its formatting uniform.

Basic Structure of C Program

Section Description
Documentation Includes the program’s description, the programmer’s name, and the creation date. The majority of these are written as comments.
Link Includes the program’s description, the programmer’s name, and the creation date. The majority of these are written as comments.
Definition preprocessor directive that contains symbolic constants is included. For instance, #define enables the usage of constants in our code. In the code, it substitutes its value for each constant.
Global Declaration Includes the declaration of functions, static global variables, and global variable declarations.
Main() Function Every C program’s main() function serves as the starting point for execution. A main() function must be present in every C program.
Subprograms All user-defined methods should be included (functions the user provides). Both built-in functions and function definitions specified in the Global Declaration section may be included there. The main() method is what is used for them.

In order to comprehend the structure of a C program, let’s look at an example:

Example: Write a program to calculate our age.

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

Let’s implement this and check:

Code:

/** //Documentation
 * file: age.c
 * author: you
 * description: a 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;
}

Logic

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

Syntax of Structure in C

Here, we will see the syntax of structure in c in detail.

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: a program to find our age.
 */

Link

This section includes all header files.
A header file is a file that contains C declarations that can be shared among many files. We can use other people’s codes in our files due to it. Before compilation, a copy of these header files is inserted into your code.

#include 

Definition

Any statement in C that starts with the symbol "#" is referred to be a preprocessor directive. Constants are made using the #define preprocessor compiler command. The usage of constants in our code is fundamentally made possible by #define, which enables the macro declaration.

#define BORN 2000

We’ve established the constant BORN, and we’ve given it the value of 2000. In general, it is better to define the constants using capital characters. Every time the aforementioned constant BORN is used in our code, 2000 will be utilized in its place.
In order to make a source program simple to edit and compile in many execution contexts, #define is frequently employed.
There is no semicolon at the conclusion of the defined sentence.

Global Declaration

All global variables, function declarations, and static variables are contained in this section. Anywhere in the program can utilize the variables specified in this section. They get access to all of the program’s features. Consequently, they are known as global variables.

int age(int current);

Our age function, which accepts a single integer parameter and outputs an integer, has been defined.

Main() Function

This section of a C program’s structure comprises the code’s primary purpose. The main() function is when the compiler begins running code. It may make use of user-defined functions as well as built-in and global variables. The main() function’s return type is not required to be an int and can alternatively be void.

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

Here, the number 2021 has been assigned to the variable current, which has been defined. After that, we invoked the age() method, which only accepts one parameter, before using the printf() function.

Subprograms

This also applies to the user-defined programs that the main() function calls. Regardless of their sequence, user-defined functions are typically written after the main() function.
The program’s control switches to the user-defined function when it is called from the main() function, and it returns to the main() function when it meets a return statement. In this instance, we’ve developed the age() function, which only accepts the current year as an input.

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

The main function calls this function. The main function receives an integer as a response.

Conclusion
This article discussed the structure of C program, the structure of C program with example, and the syntax of structure in C language. We hope this article will help you to understand the structure of C program.

Leave a Reply

Your email address will not be published. Required fields are marked *