Structure of a C Program
Every C program is built in the same order, like a letter has a fixed format: address, date, subject, body, signature.
"Explain the basic structure of a C program with an example" is one of the most common exam questions. In this lesson you will learn its 6 sections, what goes in each one, and a complete example that uses all of them.
What You Will Learn
- The 6 sections of a C program, in the correct order.
- What each section does, in one line.
- Which sections are compulsory and which are optional.
- A full program that uses every section.
Structure of a C Program Explained
Exam definition: A C program is divided into six sections: Documentation, Link, Definition, Global Declaration, main() function, and Subprograms. Only the main() function is compulsory.
| # | Section | What it contains | Example |
|---|---|---|---|
| 1 | Documentation | Comments about the program: name, author, date, purpose | /* Area of circle */ |
| 2 | Link | Header files the program needs | #include <stdio.h> |
| 3 | Definition | Symbolic constants (macros) | #define PI 3.14159 |
| 4 | Global declaration | Global variables and function declarations, usable everywhere | float area(float r); |
| 5 | main() function | Where the program starts. Has a declaration part and an executable part | int main(void) { ... } |
| 6 | Subprograms | User-defined functions called from main() | float area(float r) { ... } |
Memory trick: "Doctor Likes Drinking Green Mango Shakes" = Documentation, Link, Definition, Global, Main, Subprograms.
Compulsory or optional?
- Compulsory: only the
main()function. A program cannot run without it. - Almost always needed: the Link section, because we need
stdio.hforprintf(). - Optional: Documentation, Definition, Global declaration and Subprograms.
The two parts inside main()
- Declaration part: where variables are created, e.g.
float radius; - Executable part: the statements that do the work, e.g.
printf(...)
Syntax: General Structure of a C Program
/* 1. Documentation section: comments about the program */
#include <stdio.h> /* 2. Link section */
#define NAME value /* 3. Definition section */
int global_variable; /* 4. Global declaration section */
void function_name(void);
int main(void) /* 5. main() function section */
{
/* declaration part */
/* executable part */
return 0;
}
void function_name(void) /* 6. Subprogram section */
{
/* statements */
}
Example 1: C Program Showing All 6 Sections
This program finds the area of a circle. Each section is marked with a comment.
/* Program: Area of a circle
Author : StudyFame
Purpose: Show the structure of a C program */ /* 1. Documentation */
#include <stdio.h> /* 2. Link */
#define PI 3.14159 /* 3. Definition */
float area(float r); /* 4. Global declaration */
int main(void) /* 5. main() */
{
float radius = 5.0f; /* declaration part */
printf("Radius = %.1f\n", radius); /* executable part */
printf("Area of circle = %.2f\n", area(radius));
return 0;
}
float area(float r) /* 6. Subprogram */
{
return PI * r * r;
}
Output
Radius = 5.0 Area of circle = 78.54
Code Explanation
- Documentation: the comment at the top tells the reader what the program does. The compiler ignores it.
- Link:
#include <stdio.h>brings inprintf(). - Definition:
#define PI 3.14159means "wherever you see PI, use 3.14159". - Global declaration:
float area(float r);tells the compiler that a function namedareaexists. It takes afloatand returns afloat. - main(): creates
radiuswith value 5, prints it, then callsarea(radius)and prints the answer. - Subprogram:
area()calculates π × r × r = 3.14159 × 5 × 5 = 78.53975, which prints as 78.54 because of%.2f.
How It Works
- The preprocessor runs first: it adds
stdio.h, replacesPIwith3.14159and removes the comments. - The compiler reads the global declaration, so it already knows about
area()whenmain()calls it. - When the program runs, execution starts at
main(), not at the top of the file. main()callsarea(). Control jumps toarea(), calculates the result, and comes back tomain()with the answer.return 0;ends the program.
Example 2: The Smallest Valid C Program
Only main() is compulsory, so this is a complete, valid C program:
int main(void)
{
return 0;
}
Output
(nothing is printed)
It compiles and runs, but does nothing. As soon as you want to print something, you need the Link section (#include <stdio.h>).
Important Points to Remember
- A C program has 6 sections.
- Only
main()is compulsory. - Execution always starts from
main(), wherever it is written in the file. main()has two parts: declaration and executable.- Lines starting with
#(Link and Definition) do not end with a semicolon.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
| Calling a function before declaring it | Error or warning: implicit declaration of function | Declare it in the global section, or write it above main() |
#define PI 3.14; (semicolon) | The ; becomes part of PI and breaks expressions | No semicolon after #define |
Writing two main() functions | Error: redefinition of 'main' | Only one main() per program |
Putting #include after main() | printf is not known inside main() | Header files go at the top |
Structure of C Program Practice Questions
- Name the 6 sections of a C program in order.
- Which section is compulsory?
- What is written in the documentation section?
- What is the difference between the link section and the definition section?
- What are the two parts of the
main()function?
Show answers
- Documentation, Link, Definition, Global declaration, main(), Subprograms.
- Only the
main()function. - Comments about the program: name, author, date and purpose.
- The link section includes header files (
#include). The definition section creates symbolic constants (#define). - The declaration part and the executable part.
Coding Exercise
Write a program with all 6 sections that calculates the area of a rectangle. Use #define for the unit text ("sq cm"), and keep a global variable int count = 0; that your function increases each time it is called. Print the area for length 8 and width 5.
Show solution
/* Program: Area of a rectangle (6 sections) */ /* Documentation */
#include <stdio.h> /* Link */
#define UNIT "sq cm" /* Definition */
int count = 0; /* Global declaration */
int area(int length, int width);
int main(void) /* main() */
{
int result = area(8, 5);
printf("Area = %d %s\n", result, UNIT);
printf("area() was called %d time\n", count);
return 0;
}
int area(int length, int width) /* Subprogram */
{
count = count + 1;
return length * width;
}
Area = 40 sq cm area() was called 1 time
Quick Quiz: Structure of a C Program (MCQ)
-
Which section of a C program is compulsory?
A) Documentation B) Link C) main() function D) Subprograms
-
#include <stdio.h>belongs to which section?A) Definition B) Link C) Global declaration D) Documentation
-
#define PI 3.14belongs to which section?A) Definition B) Link C) main() D) Subprograms
-
Program execution starts from:
A) The first line B) The documentation section C)
main()D) The last function -
User-defined functions are written in which section?
A) Link B) Definition C) Subprograms D) Documentation
Show answers
- C: the
main()function. - B: the Link section.
- A: the Definition section.
- C:
main(). - C: the Subprograms section.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Number of sections | 6 |
|---|---|
| Order | Documentation → Link → Definition → Global → main() → Subprograms |
| Compulsory section | Only main() |
| Link section | #include header files |
| Definition section | #define constants |
| Parts of main() | Declaration part + executable part |
| Execution starts at | main() |
Frequently asked questions
- Which section of a C program is compulsory?
- Only the
main()function. All other sections are optional. - What is the use of the global declaration section?
- It declares global variables and functions that can be used anywhere in the program.
- Does a C program start from the first line?
- No. It always starts from
main(). - Why don't #include and #define end with a semicolon?
- They are preprocessor directives, not C statements.
Remember
- Order trick: D-L-D-G-M-S ("Doctor Likes Drinking Green Mango Shakes").
- Only
main()is compulsory.
Next Topic
Next lesson: How C Program Compilation Works. You will see the 4 steps your code goes through, and the files the compiler creates at each step.