×

Compilation Process in C language

What is the Compilation Process?

 The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic or structural errors and produces the object code if the source code is error-free.

In object code or machine code, the compilation method c converts the source code that has been taken as an input. The method of compiling can be divided into four stages, i.e., pre-processing, compilation, assembly, and linking.

A pre-processor selects the raw data as an input and extracts all the statements from the code. The pre-processor takes in and interprets the pre-processor instruction. For example, if the directive is accessible in the program <stdio.h>, then the pre-processor interprets the directive and replaces it with the content of the file 'stdio.h.'

Following are the steps that a program goes through until it is translated into an executable form:

  • Preprocessor
  • Compiler
  • Assembler
  • Linker
Compilation Process in C language

Preprocessor

Source code is the code written in a text editor, and an extension is provided to the source code file ".c ». This source code is first transferred to the preprocessor, and then that code is extended by the preprocessor. The extended code is transferred to the compiler after the extension of the code.

Compiler

The code expanded by the preprocessor is passed to the compiler. The code is translated to assembly code by the compiler. Or we can say that the C compiler transforms the preprocessed code into assembler.

Assembler

With the help of an assembler, the assembly code is translated to object code. The name of the assembler generated object file is similar to that of the source file. The object file extension in DOS is ‘obj’, and the filename is 'o' in Unix. If the source file name is sample.c, the object file name would be 'sample.obj.'

Linker

All C-written programs primarily use library functions. These library functions are pre-compiled, and the library files object code is stored with the .lib or .a extension. The linker's main function is to fuse the object code from library files with the object code of our program. Often the situation occurs when the functions specified in other files are referred to by our program; then linker plays a very important role here. It connects those files' object code to our software.

So we infer that the linker's role is to link our program's object code to the library files' object code and other programs. The linker's output is the executable script. The executable file name is similar to the source file, which varies only in its extensions. In DOS, the executable file extension is '.exe', and in UNIX, the executable file can be renamed 'a.out.' For example, if we use the function printf) (in a program, the linker will add its associated code to an output file.

We try to explain you with the help of an example.

#include <stdio.h>  
 int main()  
 {  
     printf("Hello Tutorialandexample");  
     return 0;  
 }   

Output:

Compilation Process in C language

In the above flow diagram, the following steps are taken to execute a program:

Compilation Process in C language
  • First, the input file is transferred to the preprocessor, i.e., hello.c, and then the preprocessor transforms the source code to extended source code. The expanded source code extension will be hello.i.
  • The expanded source code is transferred to the compiler, and this expanded source code is converted to assembly code by the compiler. The assembly code extension will be a hello.s.
  • It then sends this assembly code to the assembler, which transforms the assembly code into object code.
  • When an object code has been developed, the linker generates the executable file.

 

 


Related Topics

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Storage Class in C

C storage class is used to define the scope variables and function. There are four various types of storage classes that are given below. auto: The auto keyword is the default...

1 minute read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.