C Structures (structs)

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.
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
Section | Description |
---|---|
Documentation | Consists of the description of the program, programmer's name, and creation date. These are generally written in the form of comments. |
Link | All 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. |
Definition | Includes 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 Declaration | Includes declaration of global variables, function declarations, static global variables, and functions. |
Main() Function | For every C program, the execution starts from the main() function. It is mandatory to include a main() function in every C program. |
Subprograms | Includes 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:
Output
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.
Link
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.
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.
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.
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.
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.
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.