Hurry! Try our new Interactive Courses for FREE. 🥳   🚀
  Signup/Sign In

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.

C language Source code to 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.

C compilation process

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.


Frequently Asked Questions

Here are some frequently asked questions related to the C language compilation process.

Q1. What do you understand by the term Compiler?

It is a computer program that is used to translate the source code that is in a high-level programming language or a mid-level programming language into machine code, which can be executed and the machine can understand what to do.

Q2. What is a preprocessor?

The preprocessor is a directive used to instruct the compiler to preprocess the source code before the start of the actual compilation. There are some parts of the C language program that can be specified using the preprocessor, which is nothing but statements to be executed at the beginning of compilation.

Q3. What is the other name of the .c file?

The C language code files which have extension .c are also called the source code file.

Q4. What is the role of Linker?

The linker is used to link all the parts of the program together in order for the execution.

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.

Q5. What are the steps of compilation in the C Language?

Following are the steps of compilation in the C language:

  1. Preprocessor (preprocessing)

  2. Compiler (compiling)

  3. Assembler (assembly)

  4. Linker (linking)


Conclusion

In this tutorial, we learned how the C language code that we will be writing in the upcoming tutorials is compiled. What are the various steps followed in compilation and what happens in those steps?

If you are left confused after this, do not worry, just read it and as we move on with the tutorial, you will understand everything.