×

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

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

Volatile in C

Introduction A volatile keyword is a qualifier in C. Qualifiers are nothing but keywords which are used to modify the properties of a variable. Qualifiers are of two types: 1) Const The const type...

3 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

C Program for Extended Euclidean algorithms

The Euclidean method simply computes the GCD (greatest common divisor) of two numbers, p and q. (Let's assume) The GCD of two integers is the greatest number that divides them both....

3 minutes read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

Variable Declaration in C

What is a Variable? A Variable is nothing more than a name for a memory place where data/information can be stored. Any alphabet (from a to z or A to Z), the...

4 minutes read.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.