How C programming works

The process of developing software using C programming language involves a number of tasks/phases. To successfully develop an application/program we need to have an editor, a compiler, a linker and a loader. Most of the IDE (e.g. Code Blocks, Eclipse, Geany, etc.) for C programming provide all these necessary tools.

C is a compiled programming language. This means that we need to convert our source files (human readable text files) into object files (which can be understood by the machine: microcontroller, computer/laptop).

The process of developing an application in C is described in the picture below:

How C programming works

Image: How C programming works

Editing

The first step in developing an application in C is to write/edit the source code. The source code contains all the instructions which needs to be executed by the machine in a text format (understandable by humans).

We can use either a plain text editor (e.g. Notepad) or the editor provided by the IDE. The source code must be written as required by the C language syntax. After the source file is ready, it must be saved as a *.c file.

Compiling

In order to compile our source file we need a compiler. If we used a simple text editor for our source code, we need to install a C compiler on our machine and invoke it manually using the command prompt. An easier solution is to use an IDE (which should contain a compiler) and just invoke it by pressing a menu button.

The compilation task contains a initial processing phase of the source file. The initial phase is called preprocessing. The prepocessing is executed by a preprocessor invoked by the compiler. The preprocessor looks through the source code for all the lines which are starting with the # (hash) key. These lines are called compiler directives.

One of the compiler’s directive is to include functions which are defined externally from our source code. The preprocessor removes all the compiler directives from the source code but remembers what additional files need to be included later in the process. At the end of the preprocessing a temporary filed will be created, not visible to the user.

After the preprocessing is complete, the compiler kicks in. The compiler converts our source file into an object file. The object file is also named machine code and can be interpreted by the Central Processing Unit of the computer or microcontroller.

The object file is ready but it is missing some undefined references. These undefined references are pieces of code which have to be retrieved form a different place. In our case the undefined reference is the printf() function. We know from where to get the code for this function because it was specified by the compiler directive (#include<stdio.h>).

stdio.h is a header file (extension *.h) which, among other things, includes the declaration of our printf() function. By including the header file, we specify where to find the definition of the printf() function.

Linking

At this stage we need to put together all our files used by our application. This means that we need to have the object files and the static library files for the external functions. The static library files (*.lib) contain the definition of the external functions used in our source file. In our particular case the static library file will contain the machine code of the printf() function.

The linking is performed by a linker. It will search all the object files and replace all the undefined references with the referenced machine code within the library files. At the end of the linking process we are going to have an executable file (e.g. *.exe for Windows applications, *.hex for microcontrollers).

Loading

The final step is the loading of the program file into the computer’s memory so that it can be executed. This is performed by a loader. Usually the linked will include the loader into the executable file. When we run the executable it will trigger the loader which will load the program into the memory and the execution will begin.

For a microcontroller application the loading task is basically the reprogramming of the Flash memory with our newly created executable file (*.hex).

Test your knowledge regarding How C Programming Works by taking the quiz below:

QUIZ! (click to open)

For any questions or observations regarding this tutorial please use the comment form below.

Don’t forget to Like, Share and Subscribe!

One Response

  1. bukhaari qaal

Leave a Reply

Ad Blocker Detected

Dear user, Our website provides free and high quality content by displaying ads to our visitors. Please support us by disabling your Ad blocker for our site. Thank you!

Refresh