Structure of a C Program
Every C program follows a clean and well-defined structure.
Even though C is powerful and flexible, each program—from the smallest to the most advanced—uses the same standard building blocks.
Understanding this structure helps you write cleaner, error-free, and more professional code.
Basic Structure of a C Program
#include <stdio.h> // Header file
int main() { // Main function
printf("Hello"); // Program statement
return 0; // End of program
}
This structure stays the same in almost every C program you will write.
Parts of a C Program
1. Documentation Section (Comments)
This section contains comments that describe the program, author name, date, and purpose of the program.
/* Program: Simple Hello Program Author : Your Name Date : 2025 */
2. Link Section (Header Files)
Header files provide functions used in your program. Example:
- stdio.h → printf(), scanf()
- math.h → mathematical operations
- string.h → string functions
#include <stdio.h> #include <math.h>
3. Global Declaration Section
Variables and functions declared outside the main() function are called global declarations.
int count = 0; // Global variable void demo(); // Function declaration
4. main() Function
This is the heart of the entire program. Execution starts from the main() function.
int main() {
// Your code goes here
}
5. Variable Declaration Section
Inside the main(), variables must be declared before they are used.
int age; float salary;
6. Statement/Executable Section
This part contains actual instructions that the program runs.
printf("Welcome!");
age = 21;
salary = 15000.50;
7. User-Defined Functions (Optional)
Large programs often break tasks into different functions to make the code simpler.
void greet() {
printf("Hello User!");
}
Flow of a C Program
Documentation ↓ Link Section ↓ Global Declarations ↓ main() ↓ Variables ↓ Statements ↓ User-defined Functions
Complete Example
#include <stdio.h>
// Global Declaration
int number = 10;
// User-Defined Function
void display() {
printf("Number: %d\n", number);
}
int main() {
printf("This is a C Program Structure Example\n");
display();
return 0;
}
Practice Questions
- Explain all sections of a C program.
- Why is main() important in C?
- What is the purpose of header files?
- Write the basic structure of a C program.
- Differentiate between global and local variables.
Practice Task
Write a complete C program using all sections: comments, header files, global variables, main function, and at least one user-defined function.