This article explains about the different parts of a C program or the Structure of a C Program and not abut structure user datatype in C.
There is no specific convention defined as part of the standard C rule but in general as widely used we have a total of 7 parts in the Structure of C Program.
If you follow the same standard structure then it will be easier for you to understand others code and vice versa.
Let’s take a look at the whole structure.
7 Parts of a C program Structure
While I have gone through many other resources where everyone has pointed out that there are 6 parts as a whole but in fact there are 7 parts in the structure of C Program.
Every has missed the user function declaration part. It is important in C that you declare a function prototype before you can use it.
This is the very reason we include header files in a C program which only contains the function prototype or the basic declaration of a function which helps identify the compiler if we have written the predefined functions appropriately or not.
Having said that, these are the structure of C program:
- Copyright or File information section
- Header includes
- Preprocessor definitions or # defines
- Global variable declarations
- Function prototype declarations
- main() function
- User function definitions
Look at the below sample code that includes a complete structure.
/*
* filename: c-program-structure.c
* Description : Structure of C Program
*
* Copyright (c) 2024 by cprogram.dev
*/
/* Header inclusions */
#include <stdio.h>
/* Defines */
#define SITEADDRESS "https://cprogram.dev"
/* Global variable declarations */
int result;
/* Function prototype declarations */
int my_add ( int, int );
/* main() function start */
int main () {
int one, two = 0;
printf ( "Welcome to our website %s\n", SITEADDRESS )
printf ( "Enter 2 numbers\n" );
scanf ("%d%d", &one, &two);
result = add (one, two);
printf ( "Addition of these two numbers is: %d\n", result );
return 0;
}
/* User defined function definitions */
int add ( int a, int b ) {
return a+b;
}
/* END of C Program */
Code language: PHP (php)
I will explain everything in detail with examples.
1. Copyright or File Information
The first part of the C program usually contains the file information or copyright information.
/*
* filename: c-program-structure.c
* Description : Structure of C Program
*
* Copyright (c) 2024 by cprogram.dev
*/
Code language: JSON / JSON with Comments (json)
As of now you might be learning C. When you will become a pro C programmer and get hired by a company, you will see almost every C or Header file in the project will have similar initial information written.
It is always a good practice to add the description of the C file, if you are working for a company, Copyright information is a must.
When I was working in Cisco, updating the Copyright information based on the year is a mandatory affair if I have made any changes to the code. Otherwise the automated code checking will fail my commit submission.
2. Header includes
The standard C comes with many built-in libraries for mathematical functions, input/output functions such as printf(), scanf() etc. string functions such as string length strlen(), string compare strcmp() etc.
Standard C mandates that we use the function’s prototype before we use it in any of our C program.
For standard libraries, we do not need to remember their prototypes rather we just include the standard header files.
/* Header inclusions */
#include <stdio.h>
Code language: CSS (css)
In the above C program I used printf() and scanf() which are input/output functions. To add their function prototypes I have added <stdio.h>
The ‘<‘ ‘>’ mentions that the C compiler will search for this header file in the system’s include directory and not in the local directory where you are storing the C program file.
3. Preprocessor defines
If you have any #define MACROS in you C program then you need to keep it after the #includes.
/* Defines */
#define SITEADDRESS "https://cprogram.dev"
Code language: PHP (php)
Immediately after the #include field you have to fill any #define things.
4. Global Variable Declarations
After the #defines you have to add all the global variable declarations.
/* Global variable declarations */
int result;
Code language: JavaScript (javascript)
I have a very small program which does not make use of this global variable placement appropriately.
But in certain genuine cases you may need to add a global variable which should be accessible by all the functions.
This field also includes all user defined datatypes such as typedef, global structures etc.
5. User defined function declaration or prototypes
If you are defining a function of your own, then the function declaration or the prototype of the function must be declared before the main() function. Because we will be calling the function inside main() function only.
Function prototype means, what will be the name of the function, what datatype the function will take as parameters and what datatype the function will return.
/* Function prototype declarations */
int my_add ( int, int );
Code language: JavaScript (javascript)
If you leave adding a prototype of a function you defined by yourself, modern day compilers will give a warning but still move on because the definition exists in the same file.
But if you have a project that is spread across multiple files, then you must add the function declaration or protype in one of the header file and that header file must be included in the c file where you are calling the function.
6. The main() function
To make it a standard practice, every C program starts with a main () function. You must write your code inside that function to execute in the order they are written.
If you write any executable statement outside main function, that will be discarded or a compilation ERROR will be thrown at you.
As we are discussing in this structure of C program, you cannot add any executable statement outside main() but you can declare any variables.
/* main() function start */
int main () {
int one, two = 0;
printf ( "Welcome to our website %s\n", SITEADDRESS )
printf ( "Enter 2 numbers\n" );
scanf ("%d%d", &one, &two);
result = add (one, two);
printf ( "Addition of these two numbers is: %d\n", result );
return 0;
}
Code language: JavaScript (javascript)
But you can create a function of your own and then add any executable code inside it which will anyway be outside main() function.
7. User defined functions
Last but not the least, you need to add all functions defined by you after the main() function.
/* User defined function definitions */
int add ( int a, int b ) {
return a+b;
}
Code language: JavaScript (javascript)
User defined functions can also be kept before the main() function and just after adding the function declaration or the function prototype.
But it is good if you follow a standard practice and keep all user defined functions at one place.
Conclusion
That’s all from my side in the explanation of Structure of C Program.
As I have stated in the beginning that there is no mention of this structure anywhere in the standard C but following this structure will help you organize your own code as well as help you understanding others code.
If you have any doubt on any concept on C Program, leave a comment for me and I will respond to it.
Happy practicing C program.