C Compilation Process

C Compilation Process, you should know

We know that machines understand the machine code (Binary Instruction). The machine code is a computer program that is specific to architecture and written in machine language.

It is very difficult for humans to write computer programs using machine language. So to avoid this issue generally people write their programs using high-level languages like C, C++, Java, ..etc.

The high-level language increases the code readability and maintainability. But the problem is that the machine does not understand the high-level language because the machine understands the machine code. This issue is similar to the situation where two-man want to talk to each other but one man speaks only Chinese and another man only Hindi.

Now you are thinking that how they will communicate if they don’t have a common language. Don’t worry they can solve their problem with the help of a translator. A translator translates their words to communicate with each other.

Similar to the translator compiler plays the role of the translator in the C programming world. It translates the  C program into machine code. Let’s see what a compiler is.

 

What Is A Compiler?

A compiler is a computer program that translates computer code written in one programming language (the source language) into another language (the target language). The name “compiler” is primarily used for programs that translate source code from a high-level programming language to a lower-level language (e.g. assembly language, object code, or machine code) to create an executable program. Source – Compiler Wikipedia.

 

Here we are only talking about the C compiler, so it translates the C program into the low-level machine code. And the process of translating source code written in a C language to low-level machine code is called a  compilation.

Generally, the Compilation Process in C involves four steps. It might be different for the different compilers.

  1. Preprocessing.
  2. Compiling.
  3. Assembling.
  4. Linking

Compilation process in C Aticleworld

 

Now, let us see all the steps involved in a compilation process in C in detail.

Preprocessing:

Preprocessing is the first stage of compilation. The C preprocessor is a macro preprocessor that transforms your program before it is compiled. These transformations can be the inclusion of header files, macro expansions, etc. For example, if you have included #include <stdio.h> header file in your source code, then at the time of preprocessing the C preprocessor will copy the “stdio.h” content in your source code.

Let’s see some phases of preprocessing,

  • Removal of Comments.
  • Expansion of Macros.
  • Expansion of the included files.
  • Conditional compilation

 

Compilation:

The next step is a compilation. The compiler takes the preprocessed code ( output of the preprocessor) and generates the assembly code. If you are working on cross-platform, then the generated assembly code would be specific to the target processor.

Some compiler has integrated assembler, so to avoid the overhead of generating the intermediate assembly instructions. It generates machine code directly.

Let’s see some phase of the compilation but it depends on the compiler,

  • Lexical analysis, and parsing.
  • Semantic analysis (syntax-directed translation).
  • Code optimization.
  • Translate the code into intermediate code i.e. in assembly language.

 

Assembly:

Basically, this is the third stage of compilation. In this stage, an assembler is used to translate the assembly instructions to an object code. Each file has its own object file and the extension of this object file would be .obj or .o depend on the machine.

The object file contains the “relocatable” machine code that is not directly executable because it is not yet committed to any specific address in memory. Here linker plays an important role and combines all the objects, resolves the cross-module references, and fixes the addresses.

If you will open the object file, you will not able to read anything because it is a binary file. But using some tool ( objdump )you can read it, for example, objdump -d main.o  to read main.o file.

 

Linking:

Basically, it is the final stage of the C compilation process and the result of this stage is the final executable program. Above I have described that the object file contains the relocatable machine code and the linker resolves the cross-module references and fixes the addresses.  So you can say that the linker performs two important task symbol resolution and relocation. I will cover this topic in my other post. If you want you can read the article “Linkage in C“.

linker in c

 

Recommended Posts for you