0
0
Cprogramming~10 mins

Compilation process in C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Compilation process in C
Write C source code (.c file)
Preprocessing: handle #include, #define
Compilation: convert to assembly (.s file)
Assembly: convert assembly to machine code (.o file)
Linking: combine .o files and libraries into executable
Executable ready
The C compilation process transforms source code step-by-step into an executable program.
Execution Sample
C
#include <stdio.h>
int main() {
  printf("Hi\n");
  return 0;
}
This simple C program prints 'Hi' and returns 0.
Execution Table
StepActionInputOutputNotes
1PreprocessingSource code with #includeExpanded code with headers includedReplaces #include and macros
2CompilationPreprocessed codeAssembly code (.s file)Converts C code to assembly instructions
3AssemblyAssembly codeObject file (.o)Converts assembly to machine code
4LinkingObject files + librariesExecutable fileCombines code and libraries into runnable program
5ExecutionExecutable fileProgram runs and prints outputProgram prints 'Hi' to screen
6ExitN/AN/AProcess ends after program finishes
💡 Program finishes execution and returns control to OS
Variable Tracker
StageInput TypeOutput Type
PreprocessingSource code (.c)Expanded source code
CompilationExpanded source codeAssembly code (.s)
AssemblyAssembly code (.s)Object file (.o)
LinkingObject files (.o)Executable file
ExecutionExecutable fileProgram output
Key Moments - 3 Insights
Why does the preprocessor run before compilation?
Because it replaces directives like #include and macros so the compiler gets full code to translate, as shown in execution_table step 1.
What is the difference between assembly and object files?
Assembly files (.s) are human-readable instructions; object files (.o) are machine code, ready for linking, as seen in steps 3 and 4.
Why do we need linking after assembly?
Linking combines multiple object files and libraries into one executable, making the program runnable, shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of the compilation step?
AAssembly code (.s file)
BObject file (.o)
CExecutable file
DExpanded source code
💡 Hint
Check the 'Compilation' row in execution_table under 'Output' column.
At which step does the program become runnable?
APreprocessing
BCompilation
CLinking
DAssembly
💡 Hint
Look at the 'Linking' step in execution_table and see what output it produces.
If the #include directive is missing, which step will fail?
APreprocessing
BCompilation
CAssembly
DLinking
💡 Hint
Refer to key_moments about preprocessing and compilation steps.
Concept Snapshot
C compilation process steps:
1. Preprocessing: expands #includes and macros
2. Compilation: converts code to assembly
3. Assembly: converts assembly to machine code
4. Linking: combines object files into executable
5. Execution: runs the program
Full Transcript
The compilation process in C starts with writing source code. The preprocessor runs first, handling directives like #include and macros, producing expanded code. Then the compiler converts this code into assembly instructions. Next, the assembler turns assembly into machine code stored in object files. The linker combines these object files and libraries into a single executable file. Finally, the executable runs, producing the program output. Each step transforms the code closer to a runnable program.