From source code to a running program
Separate editing, translation, linking, loading, and execution, and understand where a useful diagnostic can come from.
In this chapter10 sections
A source file is text that a programmer writes. A running program is something the computer executes. This chapter follows the steps between them and explains why writing valid C is only part of getting a program to run.
1. Start with source text
You write source text in an editor and save it in a file, commonly ending in .c. Saving stores the text. Building asks tools to translate and connect the program. Running asks the environment to execute the result. These are three separate actions.
#include <stdio.h>
int main(void) {
puts("Ready.");
return 0;
}This is a complete hosted C17 program. The header supplies information used during translation. main is the function called by hosted startup, and puts requests text output. If output succeeds, the call writes Ready. followed by a newline. Writing the source does not itself perform that call.
2. A common pipeline is a useful model
- Preprocess
- Translate
- Link
- Load and run
A compiler driver can arrange several stages behind one command. For example, on a suitably configured Unix-like toolchain, cc -std=c17 demo.c -o demo can request a C17 build. Actual compiler names, commands, and executable formats vary. This is an illustration of the roles, not an installation recipe or a promise that the same command works on every computer.
The C standard defines conceptual translation phases. An implementation may combine the work in ways that preserve the specified behavior. It need not leave a preprocessed file, an assembly-language file, or a separately visible object file on your disk for each build. Learn the distinctions without turning the diagram into a universal physical process.
3. Preprocessing prepares the translation unit
Preprocessing handles #include, macro replacement, and conditional inclusion. #include <stdio.h> makes the header’s information available. It does not print anything or paste an entire compiled library into your program. The header also need not be a physical file in every implementation.
The source being translated, after preprocessing and inclusion, forms a translation unit. A large program can have several translation units. Each can be translated separately, with shared declarations describing how their parts connect. Later chapters show why those declarations must agree.
4. Translation checks and transforms the program
The tools check grammar, declarations, types, and other language requirements. They may optimise and produce machine code with related information. A conventional object file stores translated material for later linking. This use of “object” in a filename is different from an object such as an integer variable in the C language model.
Optimization can remove calculations whose results are never needed, inline calls, or choose different instructions. It must respect the required behavior of a valid program. It does not promise to rescue a program with undefined behavior. A warning is useful evidence to investigate, but “no warnings” is not proof of correctness.
5. Linking connects separately prepared parts
A declaration can describe a function without giving its body in that source file. Linking connects uses with the required definitions. If a definition is missing, the build can fail even though one file translated successfully. Matching declarations are still your program’s responsibility; a linker is not a complete C type checker.
Static and dynamic linking describe different ways to connect some program components. A statically linked executable need not contain an entire operating system or every file it will ever use. Dynamic linking may involve work at link time, load time, and sometimes later runtime resolution. The simple distinction “everything happens only before running” versus “everything happens only while running” is misleading.
6. Loading and execution are later events
On an ordinary hosted system, the environment prepares an execution context, makes needed code and data available, and reaches startup that calls main. The exact loader, runtime, and process mechanisms are system-specific. Embedded freestanding C can have a different startup arrangement. An executable that was built successfully can still fail because a runtime resource is unavailable.
| Problem | Useful place to investigate |
|---|---|
| Malformed declaration | Source and translation diagnostics |
| Function definition unavailable | Declarations, definitions, and build/link inputs |
| File cannot be opened | Runtime environment, permissions, and error handling |
| Wrong bill with valid syntax | Specification, arithmetic, and program logic |
Including a header made a function declaration visible. Does that prove the appropriate function implementation is available to the final program?
Read the explanation
No. A declaration supplies interface information. The required definition or implementation must still be provided through the build and execution environment. That is why translation and linking are separate questions.
Primary references and further reading
- WG14 N1570 — public C11 committee draft: Sections 5.1.1.1–5.1.1.3, 5.1.2.2, and 7.1.2 provide the core rules used in this C17-baseline explanation. This is a public draft, not the final ISO publication.
Optional visual model: Follow source into a program
Follow source into a program
VISUAL MODEL
Connect headers, macro replacement, translation units, and external definitions. Try leaving out one definition.
The labelled excerpts belong to config.h, main.c, and scale.c. The definition choice controls whether scale.c is supplied to the build.
/* config.h */ #define FACTOR 2int scale(int);/* main.c */ #include "config.h"int main(void) { return scale(3); }/* scale.c */ #include "config.h"int scale(int n) { return n * FACTOR; }
source—Current stagepreprocess—Later stagecompile—Later stagelink—Later stageprogram—Later stageconfig.h declares scale and defines FACTOR as 2. main.c calls scale; scale.c supplies its definition when included in this build.
This is a conceptual grouping of translation phases. Separate .o files and compiler/linker executables are common toolchain choices, not required C artifacts. These tiny files need no standard library function.
Read the complete trace
- config.h declares scale and defines FACTOR as 2. main.c calls scale; scale.c supplies its definition when included in this build.
- Each source file is preprocessed separately. The header supplies declarations, and FACTOR tokens in scale.c become 2.
- The resulting translation units are analyzed and translated. A declaration lets main.c be translated without placing scale’s body in main.c.
- The external call is connected with scale’s definition from the other translation unit.
- The linked program can run. scale(3) computes 6, and main returns that value.
Before you move on
- Saving source, building a program, and running it are separate actions.
- Headers provide translation information; required definitions must still be supplied.
- An executable can build successfully and later fail because its runtime environment lacks a resource.
Keep these ideas separate
- Declaration / Linked implementation
- A visible declaration describes an interface without proving that its required implementation is available.
How would you explain a program that translates but fails because a function definition is missing?
Optional: say it aloud or keep a note. You can continue whenever you are ready.Check your understanding
Optional. Answers are not saved or sent, and reading stays unrestricted.
Read the explanation for every choice
- The complete executable body.
A declaration describes the function’s interface; its body is a definition.
- A guarantee that linking succeeds.
The build still needs the definitions required by the program.
- Information needed to check uses of the function.
✓ Correct reasoning. A suitable declaration supports checking calls, but it does not supply the missing body.
C17
Quick reference from the earlier edition
This compact reference keeps existing links and saved passages available. Read the expanded lesson above for the full context.
A conventional toolchain turns C source into an executable through the stages below. These describe a common implementation pipeline; the standard specifies translation phases and does not require four separate tools or intermediate files.
-
Preprocessing: After early source transformations replace each comment with a space, preprocessing handles directives such as
#include,#define, and conditional compilation. Headers are included and macros are expanded, producing a preprocessed translation unit for later translation. -
Compilation: The compiler translates the preprocessed source code into assembly language or intermediate code, performing syntax and semantic analysis, type checking, and code optimization.
-
Assembly: The assembler converts the assembly code into machine code, producing object files.
-
Linking: A conventional linker connects object files and needed library components, handling references according to the toolchain. Static linking incorporates selected object and library code into the executable; it does not include every possible system component. Dynamic linking can involve link-time, load-time, and later runtime binding rather than only one universal resolution stage.
This multi-stage process enables modular development, code reuse, and efficient program execution.
Ready for the next idea?
Mark this chapter as read whenever you feel ready.