Compilation and Execution Process in C
When we write a C program, the computer cannot directly understand it. So, the code needs to be translated into a language that the computer can run. This is done through a process called compilation.
The overall process from writing the code to seeing the output on screen involves multiple steps.
What is Compilation?
Compilation is the process of converting the human-readable C source code into machine-readable object code. This step is done by a special software called the compiler. If there are no syntax errors in the code, the compiler successfully generates an object file which is used in the next step (linking).
In simple words, compilation translates our C program into a format that the computer can understand.
Steps in Compilation and Execution:
- Writing the Code: We write the source code using any text editor and save it
with a
.c
extension (likeabc.c
). - Preprocessing: Preprocessor handles all
#include
and#define
statements before actual compilation begins. - Compilation: Compiler checks the code for errors and converts it into object
code (machine code). The output file is usually
abc.obj
orabc.o
. - Linking: Combines the object code with required libraries (like
stdio.h
) to create an executable file (abc.exe
). - Execution: The executable file is then run by the operating system, and you see the output.
Real-life Analogy:
Imagine you're baking a cake:
- Writing Code = Writing Recipe
- Preprocessing = Checking Ingredients
- Compilation = Mixing Ingredients
- Linking = Putting in Oven to Bake
- Execution = Eating the Cake (Final Output)
Compilation Process Summary Table:
Step | Description | Output |
---|---|---|
1. Preprocessing | Handles macros and header files | Expanded source code |
2. Compilation | Checks for errors and converts code to machine language | Object file (.obj / .o) |
3. Linking | Combines object file with standard libraries | Executable file (.exe) |
4. Execution | Runs the executable file | Program Output |
Summary:
- Compilation is the process of converting human-readable C code into machine code.
- It involves preprocessing, compilation, linking, and execution steps.
- Each step has its own role in preparing the program to run correctly.