What are the 4 steps to convert C program to Machine code?

CServer Side ProgrammingProgramming

Process of Creating and Running Programs

  • A program contains a set of instructions which was written in a programming language.

  • The programmer’s job is to write and test the program.

  • The 4 steps to convert a ‘C’ program into machine language are &miuns;

    • Writing and editing the program
    • Compiling the program
    • Linking the program
    • Executing the program

Writing and editing the program

  • ‘Text editors’ are used to write programs.

  • With the help of text editors, users can enter, change and store character data.

  • All special text editors are often included with a compiler.

  • After writing the program, the file is saved to disk.

  • It is known as ‘source file’.

  • This file is input to the compiler.

Compiling the program

  • “Compiler” is a software that translates the source program into machine language.

  • The ‘C’ compiler is divided into two separate programs.

    • Preprocessor
    • Translator

Let us first see about Preprocessor −

Preprocessor

  • Preprocessor reads the source code and then prepares it for translator.

  • Preprocessor commands start with ‘#’ symbol.

  • They tell the preprocessor to look for special code libraries and make substitutions.

  • The result of preprocessing is known as ‘translation’ unit.

Translator

  • Translator’s work is to convert the program into machine language.

  • It reads the translation unit and results in ‘object module’.

  • But it is not completely executable file because it does not have the ‘C’ and other functions included.

Linking programs

  • ‘Linker’ assembles I/O functions, some library functions and the functions that are part of source program into final executable program.

Executing Programs

  • ‘Loader’ is the software that is ready for program execution into the memory.

  • In the process of execution, the program reads the data from the user, processes the data and prepares the output.

Example1

Following example is to find the average of 3 numbers −

 Live Demo

#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;
}

Output

Enter values of a,b,c :2,4,5
Average=3.000000

Example2

The following is to compute circumference of a circle −

 Live Demo

#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);
}

Output

Enter radius of circle r=5.6
Circumference of circle c=35.184799
raja
Updated on 09-Mar-2021 07:03:14

Advertisements