The Compilation Process of C Language

The compilation and linking process of C language converts the source code of a C program we write into executable code that can run on hardware. This requires compilation and linking.The process is illustrated as follows:The Compilation Process of C Language This article explains the work done during the compilation process of C language, which is helpful for understanding the workings of header files, libraries, etc. Moreover, having a clear understanding of the compilation and linking process will greatly assist us in locating errors during programming and in leveraging the compiler’s error detection capabilities.Compilation Compilation involves reading the source program (character stream), performing lexical and syntactical analysis, and converting high-level language instructions into functionally equivalent assembly code. The compilation process of the source file consists of two main stages: preprocessing and compilation optimization.Preprocessing The first stage is the preprocessing stage, which occurs before the formal compilation stage. The preprocessing stage modifies the content of the source file based on the preprocessing directives placed in the file. For example, the #include directive is a preprocessing directive that adds the content of the header file to the .cpp file. This method of modifying the source file before compilation provides great flexibility to accommodate the constraints of different computer and operating system environments. The code required for one environment may differ from that required for another, as the available hardware or operating system may vary. In many cases, code for different environments can be placed in the same file, and then modified during the preprocessing stage to adapt to the current environment. It mainly involves the following aspects:

  • Macro definition directives, such as #define a b

For such pseudo-directives, the preprocessor replaces all instances of a in the program with b, but a as a string constant is not replaced. The #undef directive cancels the definition of a macro, preventing future occurrences of that string from being replaced.

  • Conditional compilation directives, such as #ifdef, #ifndef, #else, #elif, #endif, etc.

The introduction of these pseudo-directives allows programmers to decide which code to process during compilation by defining different macros. The preprocessor filters out unnecessary code based on the relevant files.

  • Header file inclusion directives, such as #include “FileName” or #include , etc.

Header files generally define a large number of macros (most commonly character constants) using pseudo-directives #define, and also contain declarations of various external symbols. The purpose of using header files is to make certain definitions available for multiple different C source programs. In the C source programs that require these definitions, only a single #include statement is needed, avoiding the need to repeat these definitions in the file. The preprocessor will include all definitions from the header file into its output file for the compiler to process. Header files included in C source programs can be system-provided, typically located in the /usr/include directory. In the program, they are included using angle brackets (< >). Additionally, developers can define their own header files, which are generally placed in the same directory as the C source program, in which case double quotes (

Leave a Comment