Structure of a C Program: Six Sections, Example and Diagram
The structure of a C program is the way its declarations, functions and statements are organized. A common classroom model uses six sections: documentation, header inclusion, macro definitions, global declarations, main, and other functions. This is a teaching convention, not a rule requiring every C source file to contain six blocks. In a normal hosted program, execution enters main; the other sections depend on what the program needs.

A complete example with all six sections
This program prints a label and the square of 5. It has no input to type. Run it unchanged first, then change sample_value to a small integer such as 6 and predict the result. If this is your first program, start with Hello World in C and return here for the bigger picture.
/* 1. Documentation: demonstrate a common six-section layout. */
/* 2. Header inclusion: declare printf and puts. */
#include <stdio.h>
/* 3. Macro definition: a name replaced before compilation. */
#define COURSE_LABEL "C structure"
/* 4. Global declarations: a constant and a function prototype. */
static const int sample_value = 5;
static int square(int value);
/* 5. Main function: entry point in this hosted example. */
int main(void) {
int result = square(sample_value);
puts(COURSE_LABEL);
printf("Square of %d = %d\n", sample_value, result);
return 0;
}
/* 6. Subprogram: the helper function's definition. */
static int square(int value) {
return value * value;
}
Run the six-section example online
Expected output
Reference output for the displayed program, not a live execution result.
C structure
Square of 5 = 25
The multiplication is safe for the small values shown. It is not a general-purpose checked square function: sufficiently large int operands can produce signed overflow, which has undefined behavior. A useful teaching example should state its limits, not imply that every possible input is safe.
Diagram of the six sections
Read this diagram from top to bottom as an organization of source text, not as the order in which every line executes. Preprocessor directives and function declarations are not runtime instructions.
C source file
|
+-- Documentation /* purpose and comments */
+-- Header inclusion #include <stdio.h>
+-- Macro definitions #define COURSE_LABEL ...
+-- Global declarations constant and square() prototype
+-- main() local declarations and statements
+-- Other functions square() definition
| Teaching section | Purpose in this example | Required in every source file? |
|---|---|---|
| Documentation | Explain the purpose and assumptions | No; comments help readers |
| Header inclusion (often called the link section) | Provide declarations for standard I/O functions | No; include the headers needed by the code |
| Macro definition | Give a preprocessing name to a string | No; not every constant needs a macro |
| Global declaration | Declare a file-scope constant and a helper before use | No; many small programs use only local objects |
| Main function | Provide the hosted program's entry point | Required somewhere in a hosted C program, not in every file |
| Other functions (subprograms) | Separate reusable work from main |
No; a small program may have only main |
What each section actually does
Documentation
The opening comment tells a reader why the file exists. Comments can also record input limits, ownership rules and non-obvious decisions. They do not print anything. During translation, comments are replaced by whitespace; they do not become executable statements. Avoid comments that merely repeat every obvious assignment.
Header inclusion is not library linking
#include <stdio.h> makes the standard I/O declarations available, including those for printf and puts. Textbooks sometimes call this a “link section,” but inclusion and linking are different operations. The preprocessor handles the directive; the linker later resolves references to definitions in object files and libraries. A header is not the compiled implementation of the functions it declares.
If a function is called without a suitable declaration, see the implicit declaration error guide. If it is declared but its definition cannot be found while building, see the undefined reference guide.
Definitions and declarations
#define COURSE_LABEL "C structure" defines an object-like macro. The name is replaced by its tokens before normal compilation. It is not a variable and has no variable type. A typed constant such as static const int sample_value = 5; is often preferable for numeric values when a macro is unnecessary.
The declaration static int square(int value); is a function prototype: it states the return type and parameter type before the call in main. Its semicolon ends a declaration. The later definition supplies a body in braces. static on these file-scope names limits their linkage to this translation unit; it is not what makes the variable read-only. That is the role of const here.
The main function
int main(void) means that this form of main takes no parameters and returns an int status. The local object result belongs to the function's block. The call to square computes a value; puts and printf produce output. return 0; indicates successful termination to the host environment. Modern C also treats reaching the closing brace of main as returning zero, but writing the return explicitly can make a beginner example clearer.
The braces group the statements belonging to the function. A missing closing brace can produce an end-of-input diagnostic. Indentation helps humans see nesting; it does not replace the braces.
Other functions
The square definition can appear after main because a compatible prototype was already visible. It could instead be defined before main. C does not require helper definitions at the bottom of a file, and it does not support defining ordinary functions inside other functions in standard C. For parameters, return values and prototypes, continue to functions in C.
From source file to executable
Source organization and build stages are different diagrams. A typical GCC-based build follows this conceptual flow:
source.c + included headers
|
v
preprocessing
|
v
compilation
|
v
assembly
|
v
object file(s) + libraries
|
v
linking
|
v
executable
A compiler driver normally coordinates these stages with one command. Save the full example as structure.c. On a system with GCC installed, compile it using:
gcc -std=c17 -Wall -Wextra -pedantic structure.c -o structure
On Linux or macOS, run the resulting executable with ./structure. On Windows with a suitable GCC toolchain, build with -o structure.exe and run .\structure.exe in PowerShell. The exact installation procedure depends on the operating system; an editor alone does not install a C compiler. You can also use the online C compiler without installing a toolchain.
A short exam-ready explanation
A C program can be described using six teaching sections. Documentation explains its purpose; header inclusion provides declarations; macro definitions name preprocessing tokens; global declarations introduce file-scope objects and function prototypes; main provides the hosted entry point; and other function definitions provide reusable operations. The sections are not all compulsory, and their order can vary as long as the language's declaration and definition rules are satisfied.
For a longer answer, draw the source-layout diagram, reproduce the labeled program, and explain why a prototype allows main to call a function defined later. Keep the build-stage diagram separate. There is no downloadable PDF attached to this lesson; the notes and diagrams are available directly on this page.
Frequently asked questions
Is program structure the same as struct in C?
No. Program structure describes how source code is organized. The struct keyword defines an aggregate type whose members can have different types. Learn that separate concept in structures in C.
Must every C file have main?
No. A multi-file hosted program normally has one definition of main, while other files provide functions or objects. A library source file does not need its own main. In a freestanding environment, such as some embedded systems, the entry point is implementation-defined and may follow different rules.
Are headers and global variables compulsory?
No. A program that calls no library functions may need no standard headers, and functions can keep their data local. Prefer the declarations actually needed over filling six artificial sections just to match a diagram.
What should I study next?
Connect the layout to arrays and functions. Then follow a full algorithm through a small function in linear search or insertion sort. Read the code, predict the reference output, and only then run it.