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

C Source Code Compilation Process

Ff

Uploaded by

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

C Source Code Compilation Process

Ff

Uploaded by

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

C Source Code Compilation Process

The C source code compilation process is a multi-step process,


which involves pre-processing, compiling of code, linking of
libraries, etc. In this tutorial, we will learn how the C code is
compiled into object code.

The process of converting the source code written in


any programming language(generally, mid-level or high-level
language) into machine-level language that is understandable by
the computer is known as Compilation. The software that is used
for this conversion is known as a Compiler.

In C language, through compilation, the C language source code is


converted into object code.
The compiler takes the input which is the source code and
provides the output in the form of object code. The complete
process of compilation in the C language is further divided into four
phases:

1. Pre-processing,
2. Compiling,
3. Assembling, and
4. Linking

The compiler checks the source code for any syntax or structural
errors and after checking, if the source code is found error-free, it
then generates the object code that has an extension .obj (for
Windows) or .o (for Linux).
Let us now take a look at the different stages of the compilation
process in the C language.

Compilation Process

Like mentioned above, the different stages of the compilation


process are as follows:

 Pre-processing
 Compiling
 Assembling
 Linking

In the flow chart below we have explained how the compilation


process work and what are the different stages of compiling the C
language source code.
Let's discuss all these stages of the C language source code
compilation in the order they are performed.

Step 0: Pre-processing of the source file

In this phase, pre-processing of the source file is done. The Pre-


processor is a program that accepts the C source code file and then
it performs the following tasks:

 It will remove the comments from the source code.


 It will perform the Macro expansion if any Macro is used (Do
not worry about Macros, we will learn about them later)
 It will perform the expansion of the included header files.

Step 1: Preprocessor

It is a program that processes the source program before passing


them on to the compiler. At this step the pre-processors used in any
C program are handled and the source code is made ready for
compilation.

 Each preprocessing statement must start with #, where # is


called the preprocessor directive.
 Each preprocessing directive is a single-line code statement.
 The word after # is called the preprocessor command.

Some of the preprocessor directives are as follows:

1. #include

To include a particular header using the name of the header file into
the C language program code.

2. #define

This is used to define a MACRO in the C language.


3. #error

This preprocessor command is used to print the error message.

Just like the above three, there are many other preprocessors, we
will cover them in detail in a separate tutorial.

Hence, the preprocessor expands the source code(adds the


required information) and then this expanded source code is passed
on to the compiler.
It gives the (.i) extension to the source code file which is initially with (.c) extension.

Step 2: Compiler

The expanded code by the preprocessor is then passed on to the


compiler. As we know a compiler is a program that converts
the high-level language(or mid-level language) code to
the assembly code, which is then converted into the machine code,
which the machine can understand.

Therefore the preprocessed code given by the preprocessor to the


compiler is then converted into assembly code by the compiler,
which is then passed on to the Assembler.

The source file which got the (.i) extension in the previous step gets
converted into (.s) extension by the compiler.

Step 3: Assembler

The assembler converts the assembly code that it gets from the
compiler into the object code. The extension of the file in this step
becomes (.obj).

Don't think that Assembler is a separate program generating the


object code. The Assembler is part of the compilation process of the
C language source code.
When in laymen language, we say, the C code is compiled, it means the complete compilation
process, covering all these steps, is done.

Step 4: Linker

A linker is a tool that is used to link all the parts of a program


together in order of execution. The code after this stage
becomes Executable Machine code.

There might be some situations when our program refers to the


functions that are defined in some other files. Or, if the code for
some program is too big, we can break it into two files, which will
be compiled separately and then linked using the Linker.

In the C language compilation process, the Linker plays a very


important role.

If your C program includes a header file, and you are using some
function defined in that header file, then the Linker will link the
required object code for the function in the library, to the object
code of your program and package them together.

Similarly, if your program code is too big and you break it into two
files, then both the source code files will be converted into object
code separately and then the Linker will link them and make the
code ready for execution. This is also called Separate Compilation.

You might also like