Preprocessors in C and C++ languages
Preprocessors are the term that used to describe the process that needs to be done *before* running the processor. You would probably heard this as compile-time, which is before the run-time.
As a C/C++ developer, I am using this feature most of the time that I wrote code. So I wanted to talk about it a little.
Yes, also libraries that you included into your code from the beginning are preprocessors too.
#include line will work before the compile-time, while you are writing your code. It is including the standard input and output library and allows you to use input and output operations like printf, scanf etc.
However, libraries are not the only ones that can be tought as preprocessors. Also #define keyword is an example for it.
#define PI 3
After this definition, we are allowed to use “PI” in our code, and it will automatically understand it as 3. Behind the scene, before compiling the code that you have been wrote, all of “PI” variables will be replaced with “3”. You can ask the question that, “Why do we need this anyway”. The answer is, readibility, easy refactoring and encapsulation.
In Header Files
If you read or write any C or C++ code, I bet you already heard the header file. It representation is .h
In header files, there is a small usage especially recommended by Google, and you can use this representation also in your .cpp files too, but it is not a good choice from my side.
Anyway, I am trying to tell you that, we also have #ifndef and #endif brothers.
Let me give you an example fro those.
// Some sort of codes here
// Some more
// There you go
Okay, non-sense right? Not actually. Let me describe you what I did here. Read above code in English.
If ORCUN_PROJECTAVA_H is not defined, please define it, and when you finished writing some codes in this header file, understand it with the sign of #endif ORCUN_PROJECTAVA_H.
In your .cpp file, it will work like, if some variable of something doesn’t defined yet, please go ahead and skip these parts. So some code lines can be used in multiway like in this description.
Why do I need to use these lines? Compiler doesn’t know if this is a header file or not?
Yes it can know it already, but this usage keeps you from making the mistake of redefiniton of same header files.
Let’s assume we have a header file that named as: impressionante.h
and we have a variable in it.
int il_bene = 10;
From now on, any file that includes impressionante.h file, will have the integer value il_bene = 10.
In this case, assume that in cowboys.cpp we included impressionante.h file, and
in impressionante.h header file, we have perfetto.h header file too and in this perfetto.h file, there is an another assigning process like in impressionante.h file. So there is a problem, because they will be mixed as you guessed.
How can we solve the problem?
We need to set the impressionante.h file to work just once. Which means, we need to tell to compiler to go into impressionante.h file just once.
So here is the solution.
int il_bene = 10;
With this implementation, compiler will understands that, after first include process, it will not get into #ifndef process again. So it will work once.
Hope this article helps you to unerstand the usage of preprocessors. Feel free to ask me anything if you needed.