Daniel Lorenzo Scoccia
3 min readJun 11, 2020

What happens when you type gcc main.c

What is Source Code?

Source code is the list of human-readable instructions that a programmer writes — often in a word processing program — when he is developing a program. The source code is run through a compiler to turn it into machine code, also called object code, that a computer can understand and execute. Object code consists primarily of 1s and 0s, so it isn’t human-readable.

And.. What is C?

C is a procedural programming language. It was initially developed by Dennis Ritchie in the year 1972. It was mainly developed as a system programming language to write an operating system. The main features of C language include low-level access to memory, a simple set of keywords, and clean style, these features make C language suitable for system programmings like an operating system or compiler development.

How does C compiler works?

Once you have written a source file using a text editor, you can invoke a C compiler to translate it into machine code. The compiler operates on a translation unit consisting of a source file and all the header files referenced by #include directives. If the compiler finds no errors in the translation unit, it generates an object file containing the corresponding machine code. Object files are usually identified by the filename suffix .o or .obj . In addition, the compiler may also generate an assembler listing.

Compiler

Pre-processing of source file

The C compilation begins with pre-processing of source file. Pre-processor is a small software that accepts C source file and performs below tasks.

  • Remove comments from the source code.
  • Macro expansion.
  • Expansion of included header files.

After pre-processing it generates a temporary file with .i extension. Since, it inserts contents of header files to our source code file. Pre-processor generated file is larger than the original source file.

Compilation of pre-processed file

In next phase of C compilation the compiler comes in action. It accepts temporary pre-processed <file-name>.i file generated by the pre-processor and performs following tasks.

  • Check C program for syntax errors.
  • Translate the file into intermediate code i.e. in assembly language.
  • Optionally optimize the translated code for better performance.

After compiling it generates an intermediate code in assembly language as <file-name.s> file. It is assembly version of our source code.
Let us look into compilation.s file.

Assembling of compiled source code

Moving on to the next phase of compilation. Assembler accepts the compiled source code (compilation.s) and translates to low level machine code. After successful assembling it generates <file-name.o> (in Linux) or <file-name.obj> (in Windows) file known as object file. In our case it generates the compilation.o file.

Linking of object files

Finally, the linker comes in action and performs the final task of compilation process. It accepts the intermediate file <file-name.o> generated by the assembler. It links all the function calls with their original definition. Which means the function printf() gets linked to its original definition.

Linker generates the final executable file (.exe in windows).

Daniel Lorenzo Scoccia
Daniel Lorenzo Scoccia

Written by Daniel Lorenzo Scoccia

0 Followers

Full Stack Software Developer. Freelancer.

No responses yet