- The 6 main sections of a C program
- What is the Documentation section and why it matters
- What is the Link section and header files
- What is the Definition section and constants
- What is the Global Declaration section
- What is the main() function section
- What is the Subprogram section
- How to write a complete C program with all sections
Introduction to C Program Structure
Every C program follows a specific structure. Think of it like building a house — you need a solid foundation before you can add the walls and roof. Similarly, a C program needs to be organized in a particular way to work correctly.
A well-structured C program has 6 main sections, each serving an essential purpose. Let's explore each one step by step.
1. Documentation Section
The Documentation Section is where you add comments to explain your code. It's like leaving notes for yourself and others who might read your code later.
Comments are ignored by the compiler — they're only for humans. They help anyone understand what the program does, who wrote it, and when it was created.
In C, you can write comments in two ways:
1. Single-line comment
// This is a single line comment
2. Multi-line comment
/*
This is a
multi-line
comment
*/
The Documentation section typically includes:
- Project Name — What the program is called
- Author Name — Who wrote the program
- Created Date — When it was created
- Program Description — What it does
2. Link Section
The Link Section is where you include header files. Header files contain pre-written functions that you can use in your program.
When you include a header file, the compiler inserts its code into your program before compiling. This is like borrowing tools from a toolbox — you don't need to build everything from scratch.
What is a Header File?
A header file has a .h extension and contains function declarations and macro definitions.
There are two types:
- System header files — Comes with the compiler (e.g.,
stdio.h,string.h) - User-defined header files — Written by the programmer
Common header files:
#include <stdio.h>— Standard Input/Output#include <string.h>— String manipulation#include <math.h>— Mathematical functions
3. Definition Section
The Definition Section is where you declare symbolic constants. These are values that don't change during program execution.
You use the #define keyword to create constants:
#define PI 3.14
#define g 9.8
Instead of writing the value directly in your code, you can use the constant name. This makes your code cleaner and easier to maintain.
4. Global Declaration Section
The Global Declaration Section is where you declare variables and functions that are accessible throughout the entire program.
Variables declared here are called global variables. They can be used by any function in the program — unlike local variables that are confined to a single function.
Example:
float area; // Global variable
float area(float r); // User-defined function declaration
5. main() Function Section
Every C program must have a main() function. This is where the program starts executing.
The main() function has two parts:
1. Declaration Part — Variables used in the program are declared here.
2. Executable Part — The actual code that does the work.
The program execution begins at the opening brace { and ends at the closing brace }.
Every statement in the main() function must end with a semicolon ;.
6. Subprogram Section
The Subprogram Section contains all the user-defined functions that perform specific tasks. These functions are called from the main() function.
You can place user-defined functions in any order after the main() function. Each function can be reused multiple times — write once, use many times!
Complete Program Structure Example
/* Documentation Section
Program Name: Program to find the area of circle
Author: Name of Author
Date : 12/01/2015
Time : 11 AM
*/
#include "stdio.h" // Link section
#include "conio.h" // Link section
#define PI 3.14 // Definition section
float area; // Global declaration section
void main()
{
float r; // Declaration part
printf("Enter the radius of the circle\n"); // Executable part
scanf("%f", &r);
area = PI * r * r;
printf("Area of the circle = %f \n", area);
message();
}
void message()
{
printf("This is a Sub Function \n");
printf("We can add more Sub Functions \n");
}
Frequently Asked Questions About C Program Structure
1. Is the Documentation section mandatory in C?
No. The documentation section is optional. It's good practice to add comments, but the compiler ignores them. They're for programmers, not machines.
2. Can I write a C program without any header files?
Yes, but you'll need to manually write all the functions yourself. Header files like stdio.h provide essential functions like printf() and scanf().
3. What is the difference between global and local variables?
Global variables are declared outside all functions and can be accessed anywhere. Local variables are declared inside a function and can only be used within that function.
4. Can I have multiple main() functions in a C program?
No. Every C program can have only one main() function. It's the entry point of the program.
💡 Quick tip: Always structure your C program with clear sections. It makes your code more readable, maintainable, and professional.