1. Introduction
Every C program follows a well-defined structural template. Knowing this structure helps you write syntactically correct programs quickly and is a frequently tested exam topic, since examiners often ask students to identify or label the sections of a given program.
Cricket analogy: Just as a scorecard always follows a fixed template — toss result, batting order, bowling figures — knowing a C program's structural template lets you write correct code quickly, and examiners frequently ask students to label sections of a scorecard just like they label sections of a program.
While not every section is mandatory in every program, understanding the conventional order — documentation, preprocessor directives, global declarations, the main() function, and user-defined functions — builds good coding habits from the start.
Cricket analogy: While not every scorecard section is filled for every match (no man-of-the-match if abandoned), understanding the conventional order — toss, innings, bowling figures, result — builds good scoring habits, just as the conventional order of documentation, preprocessor directives, and functions builds good C coding habits.
2. Syntax
/* 1. Documentation section (comments) */
/* 2. Preprocessor directives */
#include <stdio.h>
#define PI 3.14159
/* 3. Global declarations */
int globalCount;
/* 4. Function prototypes */
int add(int a, int b);
/* 5. main() function */
int main(void) {
/* 5a. Local declarations */
int result;
/* 5b. Statements */
result = add(2, 3);
printf("Result: %d\n", result);
return 0;
}
/* 6. User-defined function definitions */
int add(int a, int b) {
return a + b;
}3. Explanation
A well-organized C program typically has these sections in order: (1) Documentation section — comments (/* ... */ or //) describing the program's purpose, author, and date; ignored by the compiler but valuable for readability. (2) Preprocessor directives — lines starting with # (like #include and #define) that are processed before compilation to include headers or define macros/constants. (3) Global declarations — variables or function prototypes declared outside any function, visible throughout the file.
Cricket analogy: A scorecard's documentation is like the match header noting venue, date, and umpires — ignored by players but valuable for records; preprocessor directives are like the pre-match toss and pitch report that set conditions before play; global declarations are like the fixed team lineup announced before the first ball, visible for the whole match.
(4) The main() function — the mandatory entry point, containing local variable declarations followed by executable statements. Execution always starts here. (5) User-defined function definitions — additional functions (declared via prototypes earlier) that implement specific tasks and are called from main() or other functions. (6) Comments can appear anywhere in the code to explain logic, but are stripped out by the preprocessor and have no effect on execution.
Cricket analogy: The main() function is like the actual innings itself, the mandatory heart of the match where the toss winner's decision plays out ball by ball; user-defined functions are like specialist roles — a designated death-over bowler or pinch hitter — called in when main play needs them; comments are like a commentator's asides that add color but don't change the scoreboard.
Tip: Only #include and main() are strictly mandatory for a minimal valid C program. Global declarations, macros, and user-defined functions are optional depending on program complexity.
Common mistake: Forgetting a function prototype (declaration) before main() when the function is defined after main(). Without it, older compilers may implicitly assume a return type, and modern compilers (C99/C11) will raise an error or warning for implicit declarations.
4. Example
/* Program: Compute area of a circle */
#include <stdio.h>
#define PI 3.14159
float calculateArea(float radius); /* prototype */
int main(void) {
float radius = 4.5f;
float area;
area = calculateArea(radius);
printf("Area of circle: %.2f\n", area);
return 0;
}
float calculateArea(float radius) {
return PI * radius * radius;
}5. Output
Area of circle: 63.626. Key Takeaways
- A typical C program has documentation, preprocessor directives, global declarations, main(), and user-defined functions, in that order.
- #include and main() are the only strictly required elements for a minimal program.
- Preprocessor directives (starting with #) are processed before compilation.
- Local variables are declared at the start of a function/block; statements follow.
- Function prototypes before main() let you call functions defined later in the file.
- Comments (/* */ or //) are ignored by the compiler and used purely for documentation.
Practice what you learned
1. Which section of a C program is processed FIRST, before compilation begins?
2. Which of the following is the ONLY function that MUST be present in every executable C program?
3. Where should local variable declarations typically appear in a C99/C11 function?
4. What is the purpose of a function prototype placed before main()?
5. Which symbol/syntax marks a preprocessor directive in C?
Was this page helpful?
You May Also Like
Introduction to C Programming
Learn what C programming is, why it matters, and how its simple syntax and low-level power make it the foundation of modern computing.
Header Files in C
Learn how C header files work, how to write and include your own, and how include guards prevent multiple-inclusion errors.
Preprocessor Directives in C
Understand C preprocessor directives — #define, #include, #ifdef/#ifndef, and macro pitfalls — with clear examples.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics