Structure of the C Program
Last Updated :
11 Sep, 2024
The basic structure of a C program is divided into 6 parts which makes it easy to read, modify, document, and understand in a particular format. C program must follow the below-mentioned outline in order to successfully compile and execute. Debugging is easier in a well-structured C program.
Sections of the C Program
There are 6 basic sections responsible for the proper execution of a program. Sections are mentioned below:
- Documentation
- Preprocessor Section
- Definition
- Global Declaration
- Main() Function
- Sub Programs
1. Documentation
This section consists of the description of the program, the name of the program, and the creation date and time of the program. It is specified at the start of the program in the form of comments. Documentation can be represented as:
// description, name of the program, programmer name, date, time etc.
or
/*
description, name of the program, programmer name, date, time etc.
*/
Anything written as comments will be treated as documentation of the program and this will not interfere with the given code. Basically, it gives an overview to the reader of the program.
2. Preprocessor Section
All the header files of the program will be declared in the preprocessor section of the program. Header files help us to access other’s improved code into our code. A copy of these multiple files is inserted into our program before the process of compilation.
Example:
#include<stdio.h>
#include<math.h>
3. Definition
Preprocessors are the programs that process our source code before the process of compilation. There are multiple steps which are involved in the writing and execution of the program. Preprocessor directives start with the ‘#’ symbol. The #define preprocessor is used to create a constant throughout the program. Whenever this name is encountered by the compiler, it is replaced by the actual piece of defined code.
Example:
#define long long ll
4. Global Declaration
The global declaration section contains global variables, function declaration, and static variables. Variables and functions which are declared in this scope can be used anywhere in the program.
Example:
int num = 18;
5. Main() Function
Every C program must have a main function. The main() function of the program is written in this section. Operations like declaration and execution are performed inside the curly braces of the main program. The return type of the main() function can be int as well as void too. void() main tells the compiler that the program will not return any value. The int main() tells the compiler that the program will return an integer value.
Example:
void main()
or
int main()
6. Sub Programs
User-defined functions are called in this section of the program. The control of the program is shifted to the called function whenever they are called from the main or outside the main() function. These are specified as per the requirements of the programmer.
Example:
int sum(int x, int y)
{
return x+y;
}
Structure of C Program with example
Example: Below C program to find the sum of 2 numbers:
C
// Documentation
/**
* file: sum.c
* author: you
* description: program to find sum.
*/
// Link
#include <stdio.h>
// Definition
#define X 20
// Global Declaration
int sum(int y);
// Main() Function
int main(void)
{
int y = 55;
printf("Sum: %d", sum(y));
return 0;
}
// Subprogram
int sum(int y)
{
return y + X;
}
Explanation of the above Program
Below is the explanation of the above program. With a description explaining the program’s meaning and use.
Sections | Description |
---|
/** * file: sum.c * author: you * description: program to find sum. */ | It is the comment section and is part of the description section of the code. |
---|
#include<stdio.h> | Header file which is used for standard input-output. This is the preprocessor section. |
---|
#define X 20 | This is the definition section. It allows the use of constant X in the code. |
---|
int sum(int y) | This is the Global declaration section includes the function declaration that can be used anywhere in the program. |
---|
int main() | main() is the first function that is executed in the C program. |
---|
{…} | These curly braces mark the beginning and end of the main function. |
---|
printf(“Sum: %d”, sum(y)); | printf() function is used to print the sum on the screen. |
---|
return 0; | We have used int as the return type so we have to return 0 which states that the given program is free from the error and it can be exited successfully. |
---|
int sum(int y) { return y + X; } | This is the subprogram section. It includes the user-defined functions that are called in the main() function. |
---|
Steps involved in the Compilation and execution of a C program:
- Program Creation
- Compilation of the program
- Execution of the program
- The output of the program
Read more about Compiling a C Program Compilation – Behind the Scenes
Conclusion
In this article points we learned about the structure of the C Program are mentioned below:
- The basic structure of a C program is divided into 6 parts which makes it easy to read, modify, document, and understand in a particular format.
- Debugging is easier in a well-structured C program.
- There are 6 sections in a C Program that are Documentation, Preprocessor Section, Definition, Global Declaration, Main() Function, and Sub Programs.
- There are certain steps while compilation and executing of C program as mentioned below:
- Creation of Program
- Compilation of the program
- Execution of the program
- Output of the program
Similar Reads
Structure Pointer in C
A structure pointer is a pointer variable that stores the address of a structure. It allows the programmer to manipulate the structure and its members directly by referencing their memory location rather than passing the structure itself. In this article let's take a look at structure pointer in C.
3 min read
Output of C programs | Set 63
Prerequisite : Structure padding, integer promotion and sequence points. Q1. Consider the following code: C/C++ Code #include<stdio.h> struct geeks{ int i; char c; } obj; int main() { printf("%ld", sizeof(obj)); } What would be the output of the above code? A. 4 B. 5 C. 8 D. Can't be
4 min read
What are the C programming concepts used as Data Structures
Data Types Data-type in simple terms gives us information about the type of data. Example, integer, character, etc. Data-types in C language are declarations for the variables. Data-types are classified as: Primitive or Built-in data types Some of the examples of primitive data types are as follows
10 min read
C Structures
In C, a structure is a user-defined data type that can be used to group items of possibly different types into a single type. The struct keyword is used to define a structure. The items in the structure are called its member and they can be of any valid data type. Example: [GFGTABS] C #include <s
9 min read
Compiling a C Program: Behind the Scenes
The compilation is the process of converting the source code of the C language into machine code. As C is a mid-level language, it needs a compiler to convert it into an executable code so that the program can be run on our machine. The C program goes through the following phases during compilation:
4 min read
Nested Structure in C with Examples
Pre-requisite: Structures in C A nested structure in C is a structure within structure. One structure can be declared inside another structure in the same way structure members are declared inside a structure. Syntax: struct name_1{ member1; member2; . . membern; struct name_2 { member_1; member_2;
9 min read
C Program to Implement Priority Queue
Priority queue is an abstract data type(ADT) in the computer science which is designed to the operate much like the regular queue except that each element has the certain priority. The priority can determines the order in which elements are dequeued - elements with the higher priority are removed fr
6 min read
C Programming Language Tutorial
C is a general-purpose, procedural, and middle-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It is also known as the "mother of all programming languages" as it influenced many modern programming languages like C++, Java, Python, and Go. Why learn C?The C la
5 min read
Array of Structures vs Array within a Structure in C
Both Array of Structures and Array within a Structure in C programming is a combination of arrays and structures but both are used to serve different purposes. Array within a StructureA structure is a data type in C that allows a group of related variables to be treated as a single unit instead of s
5 min read
Memory Layout of C Programs
The memory layout of a program refers to how the programâs data is stored in the computer memory during its execution. Understanding this layout helps developers manage memory more efficiently and avoid issues such as segmentation faults and memory leaks. A C program's memory is organized into speci
6 min read