Structure of a C Program
Structure of a C Program
Introduction
Before writing complex programs, it is important to understand how a C program is structured. Just like a building needs a proper design, a C program also follows a specific structure.
Understanding this structure helps you write clean, organized, and error-free code.
Basic Structure of a C Program
A C program is generally divided into the following sections:
- Documentation Section
- Link Section
- Definition Section
- Global Declaration Section
- Main Function
- Subprograms (User-defined Functions)
Simple Example Program
// Program to calculate area of circle#include <stdio.h>
#define PI 3.14
int main() {
int radius = 5;
float area;
area = PI * radius * radius;
printf("Area of Circle = %f", area);
return 0;
}
Output
Area of Circle = 78.500000
Detailed Explanation of Each Section
1. Documentation Section
- Contains comments about the program
- Helps others understand your code
👉 Example:
// Program to calculate area of circle
2. Link Section
- Includes header files
- Provides predefined functions
👉 Example:
#include <stdio.h>
3. Definition Section
-
Defines constants using
#define
👉 Example:
#define PI 3.14
4. Global Declaration Section
- Declares global variables and functions
- Accessible throughout the program
👉 Example:
int x;
5. Main Function (Heart of Program)
-
Execution starts from
main() - Every C program must have it
👉 Example:
int main() {
// code
}
6. Subprograms (User-defined Functions)
- Functions created by the user
- Used for modular programming
👉 Example:
void display() {
printf("Hello");
}
Flow of Execution
👉 The program runs in this order:
- Preprocessor directives
-
main()function - Other functions
Important Rules to Remember
-
✔ Every program must have
main() -
✔ Statements end with
; - ✔ Use proper indentation
- ✔ Include necessary header files
Common Mistakes
-
❌ Missing
#include <stdio.h> -
❌ Incorrect
main()syntax -
❌ Forgetting
return 0; - ❌ Missing semicolons
Pro Tips
- ✔ Write code in proper structure
- ✔ Use comments for clarity
- ✔ Break large programs into functions
- ✔ Practice writing structured programs
Conclusion
Understanding the structure of a C program is essential for writing efficient and organized code. It forms the backbone of all C programs and helps you build strong programming skills.
Once you master this, writing complex programs becomes much easier.
👉 This article is part of Dharani Tech Edu Hub — where learning programming is made simple and practical.
Comments
Post a Comment