December 9, 2024

C Program – Behind the Scene

You like movies? You like behind the scene videos? Similarly are you curious to know about how C Program will be compiled ? How C application is executed? Let me walk through these details.

I have written below C program. We will check how this program is compiled. How executable is generated.

//This is my simple C program.
#include <stdio.h>

int main()
{
    printf("Hello World");
    return 0;
}

Try to understand these terminology.
Source Code: Code that we are writing. In this case, we are writing C program.
Source File: This is a file, in which we have written source code using C Programming. Extension of this file should be .c Below has the details about various steps that how C program is compiled by a compiler and how it executes.

C Compiler Process

1: Preprocessor
Before the actual compilation process, source code written in C program will be processed. Preprocessor is a program which is called during the first stage of the compilation process and it will process the source code. Preprocessor will remove the new lines, spaces and comments from the source code. In the above program comment //This is my simple C program will be removed.
Preprocessor scans the file for the preprocessor directive(#). While scanning the above program, it gets the line: #include Then the preprocessor will replace this line with the content of that particular file(In our case, stdio.h) Then at the end, preprocessor will generate optimized version of the source code. Extension of the file will be .i Then the optimized version of the source code will be given to the compiler.

2: Compiler
Compiler gets the optimized source code from the preprocessor and it compiles this code. If there is any syntax error, then the compiler will detect the error and displays the error in console. You need to fix those errors. If there are no syntax error, then the compiler will create assembly code(assembly level instructions)which are specific to the target processor architecture. File extension will be .s This file will be the input for the next step: Assembler

3: Assembler
Assembler will convert the assembly level instructions to machine code(This is also known as Object Code). Its extension in windows is .obj, where as in linux/Unix machine, extension is .o

4:Linker
Till now we have included only header file which contains function declaration. Functions like printf, scanf used in our program still have no definition. They will be defined in some library files. So, linking is the final stage of compilation, which takes all the static libraries and object files(generated in the above step) and generates single executable. In the windows environment, it will generate exe format. In other OS, it will generate the respected executable extension.

Note: Most of the cases, instead of writing c program in a single file, you will write program in multiple files. At that time, after compiling all the c files, you will get many .obj( or .o files in Linux/Unix environment) files. You can picturize as in the below image.

5:Loader
Loader will take the executable and it will load it to primary memory(RAM) and then the processor will execute it. If there are any logical error, then the details will be displayed in the console. If there are no run time error, then we will get the output.

Leave a Reply

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