Introduction to Behind the Scenes

While discussing and building our first C program (i.e., in the last tutorial), I explained how to execute a program. Executing a program involves two steps: build and run. To build and run the code, we have dedicated buttons available in Code::Blocks. We can click them and our code will magically produce the output we desired. But it’s not magic, to be honest; there’s a lot that goes on behind the scenes when you click those buttons. In this tutorial, I will give you a brief overview of the whole process that makes executing your code possible. In the upcoming tutorials, we will dive deeper into all the elements that make up the process. So, let’s dive in.


The Process

The process of build and run can be divided into 5 stages:

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

All these stages can be implemented using software programs. We have a preprocessor to do preprocessing, a compiler, an assembler, a linker and a loader to do compiling, assembling, linking, and loading respectively.

The code that we write in C (also called the source code) is provided as the input to the preprocessor. The Preprocessor translates it into the expanded source code. The Compiler translates the expanded source code into assembly code. This code is then provided to the assembler which in turn produces object code. Object code is then received by the linker, which is responsible for producing the executable that can run on our computers. Loader loads the executable into the main memory and processor of our computer runs it producing the desired output.

The entire process can be summarised in the following diagram:

The Program Execution Process

The process of getting the executable from the source code is called build, and getting the output from the executable is called run as shown in the figure.

In the coming tutorials, we will understand the importance of each stage and learn how different software programs work together, producing the executable from the source code as the result. For this purpose, I want you to open the Code::Blocks IDE (or whatever IDE you prefer), create an empty file and save it as example.c in the folder named C Programs (Although not mandatory, but to follow along, create this folder in preferably the Desktop of your computer. This is the place where we will keep our source files), and write the following code in it:

#include <stdio.h> 
#define N 10 
int main() { 
    printf("Value of N is %d", N); // prints the value of N 
    return 0; 
}

Do not worry about %d in the printf() function. It is one of the format specifiers available in C programming language, and here it is acting as the placeholder for the value of N. So, at the time of execution, %d will be replaced by the value of N. We will learn about format specifiers in great detail later.

Through the above C program, we will learn how a program goes through each stage of build and is eventually turned into an executable which we can run on our computers. This chapter is entirely dedicated to helping you understand what goes behind the scenes of executing a program. I personally believe, getting into the depth of these concepts as programmers is our responsibility.

In the next tutorial, we will begin with the first stage — preprocessing. So, I will see you there 🙋



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.