What is Compilation Process in C Program?

Introduction to the Compilation Process in C Programming

In the realm of C programming, the compilation process is an essential step that transforms human-readable code into machine-executable instructions. The source code, written in C, is passed through several stages before it becomes an executable program.

Initially, the process of compilation is initiated by the programmer’s invocation of the compiler with the source code as its input. The code is then processed through a sequence of phases, each critical to the development of the final executable. These phases include preprocessing, where directives are executed and macros are expanded; compiling, where the preprocessed code is converted to assembly code; assembling, where assembly code is translated into machine code; and linking, where all pieces of the program are linked together to form a complete executable. Each stage is characterized by its specific transformations and outputs, which are seamlessly integrated to ensure the accurate translation of the original C program into a form that the computer can directly execute.

What is a compilation?

It is a process of converting the source code (i.e high level language) into object code (i.e machine language). This conversion process of object code is done with help of the compiler. The compiler checks the source code for the syntactical errors, and if there is no error, then it generates the object code.

Steps in Compilation Process in C Program

In compilation following four stags involved in C/C++ language , will discuss each in details

  • Preprocessor
  • Compiler
  • Assembler
  • Linker
Compilation Process in C Program

Preprocessor is first steps in compilation . In this stage *.c file expands code and pass to compiler for compilation. In preprocessor three main activity preform on compilation file.

  • Source file inclusion
  • MACRO expansion
  • Conditional compilation
  • Removal of Comments

In source file inclusion #include replace by actual source code as part of preprocessor directive. Example – #include<stdio.h> is replaced by actual stdio.h code and get part of *.c

In MACRO expansion replace macro in code with respective value. Example – As macro #define MAX 5 , in code replace everywhere MAX with 5

In Conditional compilation code check for directive condition #ifdef .. and check what need to compile .

In Preprocessor stage it remove commented code ( /* */ or // )

In this step code get compiled by compiler . It check for syntax error , if there is any error it report and terminate compilation otherwise compile fi le and pass to assembler. Compiler convert hello.c (as example) file to hello.s which is assembly level instruction.

Assembler convert assembly level instruction to object code i.e machine understand able language. In assembler hello.s code get converted into object file hello.obj

This is final steps of compilation stage where linking of all function definitions get done. In this stage object code get linked with standard library and other object code (if pass during compilation). Standard library it is pre-compiled and stored with ‘.so’ or ‘.a’ extension in Linux. The main role of linker is to combine the generated object code of program with object code of library fi les. The output of the linker is the executable file (.exe in Windows and ./a.out in Linux) which directly run on system.

Conclusion

In summary, the compilation process in C programming is marked by its structured and detailed approach to converting source code into an executable file. The process is primarily carried out through the passive execution of sequential stages, initiated by the programmer but largely managed by the compiler and its associated tools. From preprocessing to linking, each phase is meticulously designed to handle specific aspects of the translation, ensuring that the final executable is both functional and efficient. This systematic transformation is critical not only for the execution of C programs but also for understanding the underlying mechanisms that enable high-level code to interact with computer hardware. As such, the compilation process remains a fundamental aspect of C programming, vital for both novice and experienced programmers to grasp as they develop and refine their coding skills.

Leave a Comment

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

Scroll to Top