Bird
Raised Fist0
Cprogramming~10 mins

Compilation process in C - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following is the correct order of steps in the C compilation process?
easy
A. Preprocessing, Compilation, Assembly, Linking
B. Compilation, Preprocessing, Linking, Assembly
C. Linking, Assembly, Compilation, Preprocessing
D. Assembly, Linking, Preprocessing, Compilation

Solution

  1. Step 1: Understand the compilation stages

    The C compilation process starts with preprocessing, where directives like #include are handled.
  2. Step 2: Follow the correct sequence

    After preprocessing, the code is compiled into assembly, then assembled into machine code, and finally linked to create an executable.
  3. Final Answer:

    Preprocessing, Compilation, Assembly, Linking -> Option A
  4. Quick Check:

    Compilation steps order = A [OK]
Hint: Remember: Preprocess first, then compile, assemble, and link [OK]
Common Mistakes:
  • Mixing up the order of compilation steps
  • Thinking linking happens before assembly
  • Confusing preprocessing with compilation
2. Which of the following commands runs the preprocessor only in GCC?
easy
A. gcc -o file file.c
B. gcc -c file.c
C. gcc -S file.c
D. gcc -E file.c

Solution

  1. Step 1: Identify GCC flags for compilation steps

    The flag '-E' tells GCC to run only the preprocessor and output the result.
  2. Step 2: Understand other flags

    '-c' compiles to object code, '-S' compiles to assembly, and '-o' specifies output file name.
  3. Final Answer:

    gcc -E file.c -> Option D
  4. Quick Check:

    Preprocessor only = gcc -E [OK]
Hint: Use gcc -E to run only the preprocessor [OK]
Common Mistakes:
  • Confusing -E with -c or -S
  • Using -o without understanding its purpose
  • Assuming -c runs the preprocessor only
3. What will be the output of the following commands sequence?
gcc -c file.c
gcc file.o -o program
./program
Assuming file.c contains a valid main function that prints "Hello".
medium
A. Compilation error
B. Hello
C. Linker error
D. No output

Solution

  1. Step 1: Understand the commands

    'gcc -c file.c' compiles source to object file without linking. 'gcc file.o -o program' links object file to create executable.
  2. Step 2: Run the executable

    Running './program' executes the compiled program, which prints "Hello" as per the source code.
  3. Final Answer:

    Hello -> Option B
  4. Quick Check:

    Compile, link, run = Hello output [OK]
Hint: Compile with -c, then link and run to see output [OK]
Common Mistakes:
  • Expecting output without linking
  • Confusing compile-only with full build
  • Not running the executable after linking
4. Given the code int main() { printf("Hi"); return 0; }, which error occurs if you compile with gcc file.c but forget to include #include <stdio.h>?
medium
A. Preprocessing error
B. Linker error
C. Compilation warning or error about implicit declaration
D. No error, program runs fine

Solution

  1. Step 1: Identify missing header impact

    Without #include <stdio.h>, the compiler does not know the prototype of printf, causing implicit declaration warning or error.
  2. Step 2: Understand error type

    This is a compilation warning or error, not a linker or preprocessing error.
  3. Final Answer:

    Compilation warning or error about implicit declaration -> Option C
  4. Quick Check:

    Missing stdio.h = compilation warning/error [OK]
Hint: Always include stdio.h for printf to avoid compile errors [OK]
Common Mistakes:
  • Thinking it's a linker error
  • Assuming no error occurs
  • Confusing preprocessing errors with compilation errors
5. You have two C files: main.c and utils.c. You compile them separately with gcc -c main.c and gcc -c utils.c. What is the correct command to create an executable named app linking both object files?
hard
A. gcc main.o utils.o -o app
B. gcc main.c utils.c -c -o app
C. gcc -o app main.c utils.c
D. gcc -c main.o utils.o -o app

Solution

  1. Step 1: Understand separate compilation

    Compiling with '-c' creates object files (.o) without linking.
  2. Step 2: Link object files to create executable

    Use 'gcc main.o utils.o -o app' to link object files into executable named 'app'.
  3. Final Answer:

    gcc main.o utils.o -o app -> Option A
  4. Quick Check:

    Link object files with gcc -o app [OK]
Hint: Link .o files with gcc -o executable_name [OK]
Common Mistakes:
  • Using -c flag during linking
  • Trying to compile .o files directly
  • Mixing source and object files incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard