Module 1 · Getting Started with C — topic 4 of 4
Structure of a C Program
A C source file has a predictable layout: preprocessor directives, global declarations, function definitions and main. Learn what each part does and what happens between saving a file and running it.
Learning objectives
- Identify the sections of a C source file
- Explain preprocessing, compilation, assembly and linking
- Write comments and functions in the conventional layout
Definition
A C program is one or more translation units (source files after preprocessing), each containing declarations and function definitions, exactly one of which defines the function main where execution begins.Introduction
C programs are not just a list of statements. The compiler expects declarations before use, functions defined at file level, and exactly one main. Following a standard layout makes your code readable to every C programmer.
Turning a .c file into an executable also happens in distinct stages. Knowing them helps you understand error messages: a typo in a header name fails in the preprocessor, a missing semicolon fails in the compiler, and a misspelled function name often fails only at link time.
Why it matters
When programs grow beyond one screen, structure is what keeps them understandable. Declaring helper functions above main, grouping #includes at the top and documenting with comments are habits examiners and employers both look for.
Link errors such as “undefined reference to fuction” confuse beginners. Once you know linking is a separate step that joins compiled code with libraries, the message makes sense.
Detailed explanation
1. Documentation: a comment describing the file. 2. Preprocessor directives: lines beginning with #, such as #include and #define. 3. Global declarations: constants, types and variables shared by functions. 4. Function prototypes: declarations that tell the compiler a function exists. 5. `main`: the entry point. 6. Other function definitions.
Build stages: the preprocessor expands #include and macros; the compiler checks syntax and types and emits assembly; the assembler produces an object file (.o); the linker combines object files with libraries such as the one that contains printf.
Comments come in two forms: /* block */, which can span lines, and // line (since C99). Comments do not nest: /* a /* b */ c */ ends at the first */.
Syntax
- The prototype
int square(int x);letsmaincallsquarebefore its definition appears. #define LIMIT 10replaces everyLIMITwith10before compilation.- Execution always begins at
main, wherever it appears in the file.
Code examples
A complete structured program
1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 5 squared is 25
Comments in both styles
3600 seconds in an hour
More examples
Globals, prototypes and helpers
KENHOLT Stationers Notebook 59.00 Pen set 141.60
Step-by-step explanation
#define RATE 0.18is replaced by the preprocessor whereverRATEappears.const char *SHOPis a global string usable by every function in the file.- The two prototypes let
maincall functions defined later in the file. print_linecallswith_tax, showing that functions can call other functions.%-10sprints a string left-aligned in 10 characters;%8.2fprints a number right-aligned in 8 characters with 2 decimals.
Expected output
Running “Globals, prototypes and helpers” prints exactly:
Practical applications
- Every source file in the Linux kernel follows a header comment → includes → declarations → functions layout.
- Coding standards (MISRA C, CERT C, Linux kernel style) specify file structure and comment style.
- Build systems invoke the preprocess–compile–link stages separately to recompile only changed files.
Best practices
- Start every file with a short comment describing its purpose.
- Put all
#includes at the top, standard headers first. - Declare prototypes for every function defined after
main, or define helpers above it.
Common mistakes
Calling a function before declaring it
Avoid
Prefer
C requires a declaration before use. Since C99 an undeclared call is an error; add a prototype or define the function above main.
Misspelling a library function
Avoid
Prefer
This compiles with a warning but fails at link time with “undefined reference to printff” because no library defines it.
Important notes
void main()is not standard C for hosted programs; useint main(void)orint main(int argc, char *argv[]).- Macro names such as
RATEare written in capitals by convention so they stand out from variables.
Key takeaways
- Layout: documentation, directives, globals, prototypes,
main, other functions. - Build stages: preprocess → compile → assemble → link.
- Declare functions before calling them.
Try it yourself
- 1Run
gcc -Eon a short program and look at how large the preprocessed file is. What did#include <stdio.h>add? - 2Write a program with a function
cubedefined aftermain, using a prototype.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
4 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhich build stage expands `#include` and `#define`?
- BeginnerWhat is the purpose of a function prototype such as `int square(int x);`?
- BeginnerWhich comment style was added to C in C99?