In this tutorial we will learn how to use the Preprocessor efficiently and learn some nice tips when using it. This tutorial is aimed at Beginning - Intermediate programmers.
In this tutorial we will be covering:
- Using Define's to return values
- Adding Parameters to your defines
- How to include using the #include function
- Using the preprocessor's if statements
1. Using Defines to return values.This is the primary function of a define, a macro that tells the compiler this is what I want this value to be. But, how do I use the #define macro?
CODE
#define <define name> <value>
Replace <define name> with a name of your choice, for example DRIPPING_TAP (Note that there can't be spaces), and <value> to a value of your choice. This value can be a string, integer, float, double, hex, etc value etc. But for now we are just going to use 1.
Example 1.1:
CODE
#define DRIPPING_TAP 1
Now that we have our define, we may as well use it or that would be retarded

. You can use a define of this kind like any constant variable, you can read it, you can't write to it. In example 1.2, you can see how this can be used.
Example 1.2:
CODE
#define DRIPPING_TAP 1
int main()
{
if(DRIPPING_TAP) // If DRIPPING_TAP is true
{
printf("It is true.");
}
}
Try experimenting with different values and ways they can be used, you'll be suprised how powerful the #define function really is.
2. Adding Parameters to your defines.In this section we will learn how to use our define blocks like a function. Using defines as a function generally more efficient as everything is handled by the preprocessor. The beauty of this is it doesn't require any forward declarations, and takes less memory.
We can use bitwise operations for if statements, and make use of C++'s operators effectively. Here is an example of a max() macro:
CODE
#define max(a,b) a > b ? a : b
In English: If a more than b equals true return a, else return b. This function checks two numbers, and returns the biggest one.
Say we have 3 variables called x, y and z. This is what it would look like before preprocessing.
CODE
z = max(x,y);
This is what it would look like after preprocessing.
CODE
z = x > y ? x : y;
Lets look at another macro.
CODE
#define IsNumberOdd(val) val % 2
We make the val parameter like any other function, and use the modulus sign to get the remainder of a division by 2. And because a number can only be either odd or even, you can use that for this two. Example 2.1 will demonstrate how you can make your code more clear.
Example 2.1
CODE
#define NUMBER_ODD 1
#define NUMBER_EVEN 0
#define IsNumberOdd(val) val % 2
int main()
{
int Number = 100;
if(IsNumberOdd(Number) == NUMBER_ODD) printf("%d is odd.", Number);
else if(IsNumberOdd(Number) == NUMBER_EVEN) printf("%d is even.", Number);
}
3. Including a fileMany beginners get stuck into thinking that they need to place there include files in the default directory. The most general way of including a file is
#include <..>, but you can choose to include a file inside your project's directory with the almighty preprocessor.
#include ".." will set it so the compiler will look in the project's directory not the default one.
4. Using the preprocessor's if statements.The preprocessor has a lot of useful ways of testing values. Here is an example of how you can check if the computer is running windows.
CODE
int main()
{
#if defined(WIN32)
print("Windows");
#else
print("Not windows");
#endif
}
There are a lot of other useful definitions out there, and I hope you have learnt at least something about the preprocessor from this tutorial.
