Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are the 4 steps to convert C program to Machine code?
Converting a C program to machine code is a multi-stage process that transforms human-readable source code into executable machine instructions. This process involves four essential steps that work together to create a runnable program.
The 4 Steps to Convert C Program to Machine Code
The conversion process follows these four sequential steps −
- Writing and Editing − Creating the source code
- Preprocessing − Processing directives and preparing code
- Compiling − Translating to machine language
- Linking − Creating the final executable
Step 1: Writing and Editing the Program
The first step involves creating the source code using a text editor. The programmer writes C code following proper syntax and saves it with a .c extension. This file becomes the input for the compilation process.
Step 2: Preprocessing
The preprocessor handles directives that start with # symbol. It performs text substitutions, includes header files, and processes macros. The output is an expanded source file ready for compilation.
#include <stdio.h>
#define PI 3.14159
int main() {
printf("Value of PI: %f
", PI);
return 0;
}
Value of PI: 3.141590
Step 3: Compiling
The compiler translates the preprocessed code into machine language, creating an object file (.o or .obj). This file contains machine code but is not yet executable because it lacks library functions.
Step 4: Linking
The linker combines the object file with necessary library functions (like printf, scanf) and creates the final executable program. This executable can then be run directly by the operating system.
Example: Complete Program
Here's a simple C program that demonstrates the compilation process −
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d
", sum);
return 0;
}
Enter two numbers: 5 3 Sum = 8
Conclusion
The four-step process of preprocessing, compiling, and linking transforms C source code into executable machine code. Each step serves a specific purpose in creating a runnable program from human-readable code.
