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:

  1. Documentation Section
  2. Link Section
  3. Definition Section
  4. Global Declaration Section
  5. Main Function
  6. 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:

  1. Preprocessor directives
  2. main() function
  3. 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

Popular posts from this blog

Introduction to C Programming

Operators in C

Input & Output in C