Structure of a C Program
C source files follow a loose but consistent anatomy. Once you can recognize each section on sight, reading unfamiliar C code — even a large, unfamiliar file — becomes far less overwhelming.
The typical sections
Section | Purpose |
|---|---|
Preprocessor directives | Lines starting with |
Global declarations | Variables, constants, or types visible to every function in the file. |
Function prototypes | Declarations that tell the compiler a function's name, return type, and parameters before it's used. |
The main function | The single required entry point where the program begins executing. |
Other function definitions | The actual bodies of functions declared earlier (or defined directly if used only after their definition). |
Declarations vs definitions (a preview)
A declaration tells the compiler that something exists — its name and type — without necessarily providing its full details. A definition provides the actual thing: a function's body, or the storage for a variable. int add(int a, int b); is a declaration (a function prototype); a definition adds the actual body, like int add(int a, int b) { return a + b; }. This distinction becomes important once programs span multiple files and header files, and is covered in full detail in the functions section.
A well-organized example
areas.c
/* ---- 1. Preprocessor directives ---- */
#include <stdio.h>
/* ---- 2. Global declarations ---- */
#define PI 3.14159
/* ---- 3. Function prototypes ---- */
double circleArea(double radius);
/* ---- 4. The main function ---- */
int main(void) {
double radius = 5.0;
printf("Area: %.2f\n", circleArea(radius));
return 0;
}
/* ---- 5. Other function definitions ---- */
double circleArea(double radius) {
return PI * radius * radius;
}Notice that circleArea is declared (as a prototype) before main, but defined (with its full body) after main. The prototype is what lets the compiler check the call inside main is correct, even though it doesn't see the real implementation until later in the file.