Skip to content

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.

Beginner 14 min read·Lesson 4 of 37 in C Programming

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 */.

Terminal
gcc -E prog.c -o prog.i # preprocess only
gcc -S prog.c # compile to assembly (prog.s)
gcc -c prog.c # assemble to object file (prog.o)
gcc prog.o -o prog # link into an executable

Syntax

C
/* Documentation: what the program does */
#include <stdio.h> /* preprocessor directives */
#define LIMIT 10 /* macro constant */
int square(int x); /* function prototype */
int main(void) { /* entry point */
/* statements */
return 0;
}
int square(int x) { /* function definition */
return x * x;
}
  • The prototype int square(int x); lets main call square before its definition appears.
  • #define LIMIT 10 replaces every LIMIT with 10 before compilation.
  • Execution always begins at main, wherever it appears in the file.

Code examples

A complete structured program

C
/* Prints the squares of 1 to 5. */
#include <stdio.h>
#define LIMIT 5
int square(int x);
int main(void) {
for (int i = 1; i <= LIMIT; i++)
printf("%d squared is %d\n", i, square(i));
return 0;
}
int square(int x) {
return x * x;
}
Output
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25

Comments in both styles

C
#include <stdio.h>
int main(void) {
/* A block comment can
span several lines. */
int seconds = 3600; // a line comment
printf("%d seconds in an hour\n", seconds);
return 0;
}
Output
3600 seconds in an hour

More examples

Globals, prototypes and helpers

C
#include <stdio.h>
#define RATE 0.18 /* GST rate */
const char *SHOP = "KENHOLT Stationers";
double with_tax(double amount);
void print_line(const char *item, double amount);
int main(void) {
printf("%s\n", SHOP);
print_line("Notebook", 50.0);
print_line("Pen set", 120.0);
return 0;
}
double with_tax(double amount) {
return amount * (1 + RATE);
}
void print_line(const char *item, double amount) {
printf("%-10s %8.2f\n", item, with_tax(amount));
}
Output
KENHOLT Stationers
Notebook      59.00
Pen set      141.60

Step-by-step explanation

  • #define RATE 0.18 is replaced by the preprocessor wherever RATE appears.
  • const char *SHOP is a global string usable by every function in the file.
  • The two prototypes let main call functions defined later in the file.
  • print_line calls with_tax, showing that functions can call other functions.
  • %-10s prints a string left-aligned in 10 characters; %8.2f prints a number right-aligned in 8 characters with 2 decimals.

Expected output

Running “Globals, prototypes and helpers” prints exactly:

TextOutput
KENHOLT Stationers
Notebook 59.00
Pen set 141.60

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

int main(void) { return twice(4); }
int twice(int n) { return 2 * n; }

Prefer

int twice(int n);
int main(void) { return twice(4); }
int twice(int n) { return 2 * n; }

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

printff("hi\n");

Prefer

printf("hi\n");

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; use int main(void) or int main(int argc, char *argv[]).
  • Macro names such as RATE are 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

  1. 1
    Run gcc -E on a short program and look at how large the preprocessed file is. What did #include <stdio.h> add?
  2. 2
    Write a program with a function cube defined after main, 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?
Practise this topic