Compilation process in c

  

What is a compilation?

      C is a programming language that converts source code into object code or machine code. The conversion process has four steps: Pre-processing, Compiling, Assembling, and Linking.

     The preprocessor eliminates all the comments from the source code and carries out the preprocessor directives. For instance, it substitutes the directive <stdio.h> with the contents of the 'stdio.h' file.

     The following are the phases through which our program passes before being transformed into an executable form:
  •        Preprocessor
  •        Compiler
  •        Assembler
  •        Linker

Preprocessor

A source code file is a text file that contains instructions written in the C programming language. The file name has a ".c" extension to indicate its format. Before compiling the source code, a preprocessor analyzes and modifies it according to certain directives. The preprocessor then passes the expanded code to the compiler, which translates it into executable code.

Compiler

The code which is expanded by the preprocessor is passed to the compiler. The compiler converts this code into assembly code. Or we can say that the C compiler converts the pre-processed code into assembly code.

Assembler

An assembler converts the assembly code into object code. The object file has the same name as the source file, but with a different extension. In DOS, the extension is '.obj', while in UNIX, it is 'o'. For example, if the source file is 'hello.c', the object file would be 'hello.obj' in DOS and 'hello.o' in UNIX.

Linker

C programs typically use library functions, which are pre-compiled and stored as object code files with '.lib' (or '.a') extensions. The linker's main function is to combine the object code of these library files with the object code of our program. Sometimes, our program may refer to functions defined in other files, and the linker also links the object code of those files to our program. Thus, the linker's job is to link the object code of our program with the object code of the library files and other files, and produce an executable file. The executable file has the same name as the source file, but with different extensions. In DOS, the extension of the executable file is '.exe', while in UNIX, it can be 'a.out'. For example, if we use printf() function in a program, the linker adds its corresponding code to the output file.

Let's understand through an example.


Comments

Popular posts from this blog

Introduction To C

First C Program