C Compilation Process

C Compilation Process

·

5 min read

"Hello World" is usually the first program written when starting to learn a programming language. The program prints text on the screen. Though the process might appear to take place in the blink of an eye, yet, there are various stages which the program goes through to finally display the text on the screen. These stages will be the topic of discussion.

To start writing C programs, a popular way to proceed is by installing an IDE (integrated development environment) which incorporates editing, compiling, debugging processes, all in one place. Examples of C IDE's include Visual Studio Code, Eclipse, Codeblocks.

Then, a compiler will be needed to compile the program. If it's not included in an IDE installation, one can install the compiler separately, such as the gcc compiler which stands for GCC stands for GNU Compiler Collection, free software to compile C programs. There are available resources on the internet on how to install gcc for your specific machine.

Here, I have used vim as the text editor and gcc as the compiler on a Linux command line

The text editor is used to write a program saved in a .c file extension denoting that the file is a C source code. The compiler will translate the humanly readable source code into binary code (machine-readable) to generate an executable file.

So now that the environment is set up, It is time to look into the basic structure of the C program using the "Hello World" program as an example.

Structure of a C program

#include <stdio.h>

int main()
{
   /*print text*/
   printf("Hello World);

   return 0;
}

The structure of this C program consists of:-

Preprocessor directive (#)

A preprocessor directive instructs the compiler to include the header file given.

Header file (stdio.h)

A header file is a file that consists of functions used on a program. The name of the header file used in the example is stdio.h which contains functions that perform input and output operations.

It is enclosed in angle brackets < > which indicate that the header file is an inbuilt file from C library. If it is enclosed in between double-quotes " ", this would indicate that the function is user-defined (made by the user).

Without a header file such as stdio.h, it would be impossible to use function printf().

int main()

This is called a main-function where the execution of a program begins. It contains codes that are executed line by line. The "int" indicates that is returning an integer type value once the function has been executed.

Curly braces { }

Marks the start and end of a function. It provides a function body where all codes are written within.

Comments / /

Comments are used to explain a program or a given code. It is optional to use since they are ignored during compilation. It helps to make code readable and easy to understand.

A function: printf()

There are many functions in the C library that are used to perform various tasks. In this case, printf() function is used to print a text. It is provided under the stdio.h header file, that is why it is included in the beginning (#include < stdio.h > ).

Try to run the program without the stdio.h header file and see the error message you will get

Return statement : return 0

The main function returns an (int). The integer value returned is 0 which denotes success - without errors. It marks the end of the execution of the function.

After the code is written and saved with a .c file, it is time to compile the program. Compilation of a C program happens in 4 stages

  1. Preprocessing

  2. Compiling

  3. Assembling

  4. Linking

Preprocessing

A preprocessor is a program that is used to process a file before compilation. It scans the file and

  • removes comments
  • expands the header to include the functions contained
  • where macros are used it is replaced with an actual value.

The command gcc -E filename, causes the compiler to run the preprocessor and a file with .i extension is generated. Example of the preprocessed file of the "Hello World" program used in the example

pre-proccesed-file.PNG

Compilation

Compiler converts the C source code into an assembly language. This is a low-level language that contains instructions used to manipulate computers hardware. It also checks for syntaxes errors in the file.

The command of gcc -S filename generates a .s file extension.

-S option instructs the compiler to perform preprocessing and compilation.

assembly-language.PNG

Assembling

An assembler is used to convert the file from an assembly language to a binary code (object code). The binary code is in 1 and 0's and It is not possible to view the code in a text editor. The file generated has a .o extension. The command given to generate an object code is gcc -c filename.

-c option, gets the compiler to perform preprocessing, compilation, and assembling, and a file with .o file extension is generated

Linking

Linking merges the object code with other object codes or functions to give a single executable file. Since the 'Hello World" program uses a printf() function, the object code will be dynamically linked to the program / executable file.

The command given is gcc -o hello main.c

"hello" is the new name given to the generated executable file after linking.

"main.c" is the source code of the program ("Hello world)

When "hello" is executed, it will print "Hello world" on the terminal.

The command to execute the program is ./hello

These were the stages of C compilation where appropriate commands are given to demonstrate what happens at every stage. However, everyone has to call a gcc main filenameto generate an executable file of a.aout extension. Upon execution, the program prints "Hello World".

Hoping that this brief information on the C compilation process has been helpful to you.