Home > Backend Development > C++ > body text

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

WBOY
Release: 2023-09-13 17:01:01
forward
1409 people have browsed it

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;
}
Copy after login

Output

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

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);
}
Copy after login

Output

Enter radius of circle r=5.6
Circumference of circle c=35.184799
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template