DEV Community

Gilad Ri
Gilad Ri

Posted on

Compilation & Precompilation

You, as a human being, probably know how to read in English (otherwise, you couldn't read this sentence). But, the computer doesn't. The computer's language is composed out of 0 and 1 sequences. This language is called: "Binary" and code in this language is called: "Binary code". Because none of us can read or write that language we have to have a translator, which is called "Compiler". The compiler translates our C code (we write in English) into assembly code. Assembly, in short, it's a low-level language which directly converted into executable machine code (binary code), which is a program the computer can run.

You can compile your code (translate your code to binary code) by pressing the Build button in your IDE, by running the next command in your Command Prompt (calling for the GNU Compiler directly):

gcc <your_code_file>.c
Enter fullscreen mode Exit fullscreen mode

or by using a makefile.

Anyway, it is an important thing to know, but not really our topic right now. In this post I want you to finally understand what the meaning of:

#include <stdio.h>
Enter fullscreen mode Exit fullscreen mode

or in the general format:

// If it is an external library
#include <some_external_library_name.h>

// If it is your library
#include "your_library_name.h"
Enter fullscreen mode Exit fullscreen mode

Remember the functions "printf" and "scanf"? Didn't you wonder how can you use them without to write their code down in your file? You can do that because we include their code when we wrote the command from above. This command tells the compiler to add to your code the code which is in the library . "stdio" stands for "Standart Input & Output". The ".h" suffix is a suffix of a header file. A file in which we have to write all of our function declarations. This process of adding the code from the other library happens before it's fully translated into binary code, and therefore it is called a precompilation process.

There is one more thing you can tell the compiler to do before the compilation. You can define macros (constant placeholders) that will be replaced in constant values. (As a convention, we name the macros using upper case letters, and an underline as a separator). The format for this is:

#define SOME_MACRO_IN_UPPER_CASE value
Enter fullscreen mode Exit fullscreen mode

For example:

#include <stdio.h>
#define SIZE 3

for (int i = 1 i <= SIZE; i++) {
    prinf("%d/%d", i, SIZE);
}
Enter fullscreen mode Exit fullscreen mode

This code outputs:

1/3
2/3
3/3
Enter fullscreen mode Exit fullscreen mode

Notice! don't put a semicolon (';') after the value of the macro! The value will be replaced with the semicolon, which might cause an error. For example:

#define THREE 3;

int x = THREE + 3;
Enter fullscreen mode Exit fullscreen mode

will be translated to:

int x = 3; + 3;
Enter fullscreen mode Exit fullscreen mode

It is a good thing to don't have any hard coded value (for example: 10, "hi", 'q', etc) in your code if it is not a macro. For example, if we have a lot of loops which all run 10 iterations, but we now want them all to run 100 iterations we have to replace all the 10 with 100, what can be frustrating and even dangerous, if we forget to replace in some places. Instead, we can declare a macro SIZE with the value 10 and use it in all of our loops, and then change it to 100 when we want.

For more information about compilation, you can find here in Alexandra Williams post.

Always remember, it's much C-mpler than you thought!

Regards,
Gilad

Top comments (0)