Home > Article > Backend Development > What are the four steps to convert a C program into machine code?
A program consists of a set of instructions written in a programming language.
A programmer’s job is to write and test programs.
The 4 steps to convert a 'C' program into machine language are:
Write using a text editor program.
With a text editor, users can enter, change, and store character data.
All special text editors are usually included with the compiler.
After writing the program, save the file to disk.
It is called the "source file".
This file is the input to the compiler.
"Compiler" is a program that converts source programs into machine language software.
The "C" compiler is divided into two separate programs.
Let’s look at the preprocessor first-
The preprocessor reads the source code and then prepares it for the translator.
Preprocessor commands start with the "#" symbol.
They tell the preprocessor to find a special code base and replace it.
The result of preprocessing is called "translation unit".
The job of a translator is to convert a program into machine language.
It reads translation units and generates "object modules".
But it is not a fully executable file because it does not contain "C" and other functions.
The "linker" assembles the I/O functions, some library functions and functions in the source program into the final execute program.
The "loader" is the software that prepares the program for execution into memory.
During execution, the program reads data from the user, processes the data and prepares output.
The following example is to find the average of 3 numbers-
Real-time demonstration
#include<stdio.h> int main(){ int a,b,c,d; //declaring 4 variables float e; printf("Enter values of a,b,c:"); scanf("%d,%d,%d",&a,&b,&c); //read 3 input values from keyboard d=a+b+c; e=d/3; printf("Average=%f",e); // printing the result return 0; }
Enter values of a,b,c :2,4,5 Average=3.000000
The following is the calculation of the circumference of a circle-
Real-time demonstration
#include <stdio.h> #define PI 3.1415 // defining PI value main (){ float c,r; printf("Enter radius of circle r="); scanf("%f",&r); c=2*PI*r; printf("Circumference of circle c=%f", c); }
Enter radius of circle r=5.6 Circumference of circle c=35.184799
The above is the detailed content of What are the four steps to convert a C program into machine code?. For more information, please follow other related articles on the PHP Chinese website!