Home >Backend Development >C++ >What are the four steps to convert a C program into machine code?

What are the four steps to convert a C program into machine code?

WBOY
WBOYforward
2023-09-13 17:01:011423browse

The process of creating and running a program

  • 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:

    • Writing and Editing the Program
    • Compiling the Program
    • Link program
    • Execute program

Write and edit program

  • 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.

What are the four steps to convert a C program into machine code?

Compiler

  • "Compiler" is a program that converts source programs into machine language software.

  • The "C" compiler is divided into two separate programs.

    • Preprocessor
    • Translator

Let’s look at the preprocessor first-

Preprocessor

  • 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".

Translator

  • 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.

Linker

  • The "linker" assembles the I/O functions, some library functions and functions in the source program into the final execute program.

What are the four steps to convert a C program into machine code?

Execute the 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.

What are the four steps to convert a C program into machine code?

Example 1

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

Output

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

Example 2

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

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete