How Works Compiler in C?

--

C is a compiled language, which means it must be run through a compiler. The source code then undergoes a number of steps before the compiler returns an executable program. Compiled programs provide the benefits of portability across various operating systems and execution speed and efficiency.

1. Preprocessing —

Lines starting with a # character are interpreted by the preprocessor as preprocessor commands.This is the first stage of compilation process where preprocessor directives (macros and header files are most common) are expanded. To perform this step gcc executes the command internally.

2. Compilation —

During compilation the code is translated into assembly instructions specific to the type of processor architecture. The compiler (ccl) translates main.c into main.s. File helloworld.s contains assembly code. You can explicitly tell gcc to translate helloworld.i to helloworld.s by executing the following command.

[root@host ~]# gcc -S helloworld.i

After compiling it from source to assembly language, it coverts it to binary language. But what is assembly language?

3. Assembly —

An assembly language implements a symbolic representation of the machine code needed to program a given CPU architecture. Below is an example of what assembly code looks like and what it is doing.

4. Linking —

During linking the pieces of object code is arranged so that the function in other files can call functions in some other files. It also adds back the part containing the library functions used by the program. The end result being that now you have an executable program.

--

--