Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Compilation Process in C

Last Updated on December 22, 2023 by Ankit Kochar

The compilation process in the C programming language is a fundamental step that transforms human-readable source code into machine-executable instructions. Understanding this process is crucial for programmers to create efficient and error-free applications. The compilation process involves several stages, each playing a pivotal role in converting source code into executable binaries.

What is Compilation?

Before digging deep into the classic definition of compilation, let’s first consider the following scenario: Person A speaks language 1, and Person A wants to chat with Person B, who just understands language 2, either of them now requires a translator to translate their words into another language in order to communicate with each other. This is known as translation, or the compilation process in programming.

The compilation process in C involves translating human-readable code into machine-readable code and checking the syntax and semantics of the code to identify any syntax problems or warnings in our C program. Assume we wish to run our C program that was written in an IDE (Integrated Development Environment). In that situation, it has to go through several stages of compilation (translation) to produce an executable file that a machine can understand.

Steps in the Compilation Process in C

The compilation process in C involves four steps:

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

Let us now go through each step of the compilation process in C in detail.

Preprocessing

Preprocessing is the first stage in the compilation process in the C program. Preprocessing involves the processing of the source code by the preprocessor. The preprocessor is a program that processes the source code before it is compiled by the compiler. The main purpose of preprocessing is to prepare the source code for compilation.

The preprocessing stage includes several steps:

i. Removal of comments: In a C program, comments are used to provide a broad concept about a specific statement or section of code. Actually, comments are part of the code that is discarded during the compilation process by the pre-processor because it is of no particular use to the machine.

Here is an example:-

#include

int main()
{
    // single-line comment in C
    /* This is a 
    multi-line comment in C */
    return 0;
}

ii. Expansion of macros: Macros are constant values or expressions defined in C using the #define directives. A macro call leads to a macro expansion. The pre-processor generates an intermediate file in which pre-written assembly-level instructions replace declared expressions or constants (basically matching tokens).

Few examples of macros

a). Constant macros: These macros are used to define constant values.

#define PI 3.14159

b). Conditional macros: These macros execute a block of code if a condition is met.

#define MAX(x, y) ((x) > (y) ? (x) : (y))

iii. Inclusion of files: The addition of another file containing some pre-written code into our C Program during pre-processing is known as file inclusion in C. It is accomplished through the use of the #include directive. During pre-processing, file inclusion adds the whole contents of the filename to the source code, replacing the #includefilename> directive and producing a new intermediate file.

#include 

iv. Conditional compilation: Conditional compilation is the process of running or avoiding a block of code after determining whether or not a macro (a constant value or an expression defined with #define) is defined. The preprocessor replaces all conditional compilation instructions with predefined assembly code and sends the newly expanded file to the compiler. In a C program, conditional compilation can be achieved by utilizing instructions such as #ifdef, #endif, #ifndef, #if, #else, and #elif.

Here is an example:-

#include <stdio.h>

#define DEBUG 1

int main() {
#ifdef DEBUG
    printf("Debugging is enabled\n");
#else
    printf("Debugging is disabled\n");
#endif

    return 0;
}

Output:

Debugging is enabled

Explanation: In this example, we have defined a macro DEBUG with a value of 1. We then use a preprocessor directive #ifdef to check whether the DEBUG macro is defined. If the macro is defined, the code inside the #ifdef and #else directives will be included in the compilation process. Otherwise, the code inside the #else and #endif directives will be excluded.

Compiling

In C, the compilation phase uses built-in compiler software to turn the intermediate (.i) file into an Assembly file (.s) containing assembly-level instructions (low-level code). To improve program performance, the C compiler converts the intermediate file into an assembly file.

Assembly code is a basic English-like language that is used to write low-level instructions (in micro-controller programs, we use assembly language). The compiler software parses (syntax analyses) the entire program code in one pass and reports any syntax errors or warnings in the source code via the terminal window.

Assembling

An assembler is used to translate assembly-level code (.s files) into machine-readable code (in binary/hexadecimal format). An assembler is a pre-written program that converts assembly code to machine code. It reads basic instructions from an assembly code file and translates them into machine-specific binary/hexadecimal code known as object code.

The resulting file carries the same name as the assembly file and is known as an object file with the extension .obj in DOS and .o in UNIX OS.

Linking

Linking is the process of integrating library files into our program. Library files are prepackaged files that contain the definition of functions in machine language and have the extension .lib. The object (.o/.obj) file contains some unknown statements that our operating system cannot interpret. You can think of this as a book with some unfamiliar terms in it, and you’ll use a dictionary to figure out what they mean. Similarly, we use Library Files to provide explanations for some unknown statements in our object file. The linking process creates an executable file with the extension .exe in DOS and .out in UNIX OS.

Conclusion
In conclusion, comprehending the compilation process in C is indispensable for developers aiming to write optimized, error-free, and efficient code. Each stage of compilation holds significance, from preprocessing to linking, in ensuring that the final executable runs seamlessly. Acknowledging the intricacies of this process empowers programmers to debug effectively, optimize code, and create robust applications.

FAQs Related to Compilation Process in C

Here are some frequently asked questions on the compilation process in C.

1. What is the purpose of the compilation process in C?
The compilation process in C is the process of translating the human-readable source code into machine code that can be executed by the computer. The purpose of the compilation process is to generate an executable file that can be run on the target platform.

2. What is the role of the preprocessor in compilation?
The preprocessor handles directives such as #include, #define, and #ifdef. It prepares the source code by replacing macros, including header files, and handling conditional compilation to create a modified version of the code for further processing.

3. What are object files in the compilation process?
Object files are intermediate files generated during compilation. They contain machine code generated from the source code but are not yet executable. These files undergo linking to create the final executable file.

4. How does optimization occur during the compilation process?
Compilers offer optimization techniques to enhance code performance and reduce size. These optimizations include loop unrolling, inlining functions, and dead code elimination, among others, aimed at improving code efficiency without altering functionality.

5. What are linker errors, and how do you handle them?
Linker errors occur when the linker fails to resolve symbols or references between different parts of the code. To handle them, ensure proper declaration and definition of functions/variables, include necessary libraries, and address any missing or conflicting symbols during linking.

Leave a Reply

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