0% found this document useful (0 votes)
523 views

Compilation Process in C

The C compilation process takes source code as input and converts it into executable object code through four main steps: 1) Preprocessing expands the source code and handles preprocessor directives, 2) Compiling converts the preprocessed code into assembly code, 3) Assembling converts the assembly code into object code, and 4) Linking combines the object code with standard library object codes to create the final executable file.

Uploaded by

Paul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
523 views

Compilation Process in C

The C compilation process takes source code as input and converts it into executable object code through four main steps: 1) Preprocessing expands the source code and handles preprocessor directives, 2) Compiling converts the preprocessed code into assembly code, 3) Assembling converts the assembly code into object code, and 4) Linking combines the object code with standard library object codes to create the final executable file.

Uploaded by

Paul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Compilation process in c

What is a compilation?

The compilation is a process of converting the source code into object code. It is done with the help
of the compiler. The compiler checks the source code for the syntactical or structural errors, and if
the source code is error-free, then it generates the object code.

The c compilation process converts the source code taken as input into the object code or machine
code. The compilation process can be divided into four steps, i.e., Pre-processing, Compiling,
Assembling, and Linking.

The preprocessor takes the source code as an input, and it removes all the comments from the
source code. The preprocessor takes the preprocessor directive and interprets it. For example,
if <stdio.h> directive is available in the program, then the preprocessor interprets the directive and
replace this directive with the content of the 'stdio.h' file.

The following are the phases through which our program passes before being transformed into an
executable form:

o Preprocessor
o Compiler
o Assembler
o Linker

Preprocessor
The source code is the code which is written in a text editor and the source code file is given an
extension ".c". This source code is first passed to the preprocessor, and then the preprocessor
expands this code. After expanding the code, the expanded code is passed to the compiler.

Compiler

The code which is expanded by the preprocessor is passed to the compiler. The compiler converts
this code into assembly code. Or we can say that the C compiler converts the pre-processed code
into assembly code.

HTML Tutorial

Assembler

The assembly code is converted into object code by using an assembler. The name of the object file
generated by the assembler is the same as the source file. The extension of the object file in DOS is
'.obj,' and in UNIX, the extension is 'o'. If the name of the source file is 'hello.c', then the name of
the object file would be 'hello.obj'.

Linker

Mainly, all the programs written in C use library functions. These library functions are pre-compiled,
and the object code of these library files is stored with '.lib' (or '.a') extension. The main work of the
linker is to combine the object code of library files with the object code of our program. Sometimes
the situation arises when our program refers to the functions defined in other files; then linker plays
a very important role in this. It links the object code of these files to our program. Therefore, we
conclude that the job of the linker is to link the object code of our program with the object code of
the library files and other files. The output of the linker is the executable file. The name of the
executable file is the same as the source file but differs only in their extensions. In DOS, the extension
of the executable file is '.exe', and in UNIX, the executable file can be named as 'a.out'. For example,
if we are using printf() function in a program, then the linker adds its associated code in an output
file.

Let's understand through an example.

hello.c

1. #include <stdio.h>
2. int main()
3. {
4. printf("Hello javaTpoint");
5. return 0;
6. }

Now, we will create a flow diagram of the above program:


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

o Firstly, the input file, i.e., hello.c, is passed to the preprocessor, and the preprocessor converts
the source code into expanded source code. The extension of the expanded source code
would be hello.i.
o The expanded source code is passed to the compiler, and the compiler converts this
expanded source code into assembly code. The extension of the assembly code would
be hello.s.
o This assembly code is then sent to the assembler, which converts the assembly code into
object code.
o After the creation of an object code, the linker creates the executable file. The loader will then
load the executable file for the execution.

You might also like