The C Compilation model

December 3rd, 2009 by Kevin | No Comments | Filed in C Programming

I had mentioned that the “Hello world” is the first program written in any programming language. Well, below is the program written using the C language:

/* Hello World */

#include 
main()
{
   printf("hello, world\n");
}

And the output is:

hello world
C Compilation Process

The preprocessor will remove the statements marked as comments in between the /*…*/  Then it will insert the contents of the stdio.h file from the #include .

We can either mention #include or #include “stdio.h”. The first method tells the preprocessor to search in the standard include directories. The second method tells the preprocessor to search in the local directory.

The compiler translates the C code into assembly language code which contains machine level instructions specific to the platform such as x86, ARM, etc on which the program is being run.

The compiler then takes the assembly code and converts it into a binary level code which is called an object file marked as hello.o (example)

To create the executable file, the linker combines the program with other programs whose functions the main program uses. In the above program case, the printf() function is a library function which the object file doesnt know about. So the linker links the library object file with this main object file.

(For those who are wondering about a.out format, it is a file format though now no longer used in most systerms. The name has stuck though and it stands for assembler output.)

 

Related Posts:

Tags: , ,