Compilation & Precompilation
Tags: c, beginners, compilaion, precompilation
Published: 2019-06-18 07:35:28

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](https://en.wikipedia.org/wiki/Binary_code)". Because none of us can read or write that language we have to have a translator, which is called ["Compiler"](.https://en.m.wikipedia.org/wiki/Compiler). The compiler translates our C code (we write in English) into [assembly](https://en.wikipedia.org/wiki/Assembly_language) 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](GNU Compiler Collection) directly):
```shell
gcc <your_code_file>.c
```
or by using a [makefile](https://en.wikipedia.org/wiki/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:
```cpp
#include <stdio.h>
```
or  in the general format:
```cpp
// If it is an external library
#include <some_external_library_name.h>

// If it is your library
#include "your_library_name.h"
```

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.h>. "stdio" stands for "Standart Input & Output". The ".h" suffix is a suffix of a [header file](https://www.tutorialspoint.com/cprogramming/c_header_files.htm). 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:
```cpp
#define SOME_MACRO_IN_UPPER_CASE value
```
For example:
```cpp
#include <stdio.h>
#define SIZE 3

for (int i = 1 i <= SIZE; i++) {
    prinf("%d/%d", i, SIZE);
}
```
This code outputs:
```cpp
1/3
2/3
3/3
```
__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:
```cpp
#define THREE 3;

int x = THREE + 3;
```
will be translated to:
```cpp
int x = 3; + 3;
```
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](https://medium.com/@propagandra/understanding-how-code-is-executed-464c95e24b0b) in Alexandra Williams post.

####__Always remember, it's much C-mpler than you thought!__

Regards,
Gilad