If you need a good reference, try www.cppreference.com
Preprocessor directives are instructions for the compiler. You can define keywords that are replaced by their definition.
Example:
1 2 3 4
|
#define std::string VARIABLE_TYPE
//... somwhere in code:
VARIABLE_TYPE myvar; //in this case, it declares a string
|
What this does is allow you to do two things:
1. You can use preprocessor directive to "abstract" somthing, like character codes; i.e.
#define char(1) TERMINATION
. Instead of remembering that char(1) is a termination byte, you can simply name it.
2. You can use it to define somthing used VERY OFTEN throughout all of the code, but that you might want to change later on. For example: You may want all files that the program saves to have a file extension. If you want to change the extension, all you have to do is change 1 definition!
preprocessor directives are also good for other things, but for the purposes you need them, this is good enough for now.
Here is an example of what I use preprocessor definitions for. On Linux, multi-byte characters are returned for key presses. Here are some special keys I use often:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
/* non-control keys (keys != 27):*/
#define BACKSPACE_KEY 127
#define ENTER_KEY 10
/* Control characters (int(char) == 27), as represented by a vector of multiple return codes: */
#define LEFT_KEY std::vector<int>({27, 91, 68})
#define RIGHT KEY std::vector<int>({27, 91, 67})
#define UP_KEY std::vector<int>({27, 91, 65})
#define DOWN_KEY std::vector<int>({27, 91, 66})
#define HOME_KEY std::vector<int>({27, 79, 72})
#define END_KEY std::vector<int>({27, 79, 70})
#define PGUP_KEY std::vector<int>({27, 91, 53, 126})
#define PGDOWN_KEY std::vector<int>({27, 91, 54, 126})
#define DELETE_KEY std::vector<int>({27, 91, 51, 126})
|
Instead of remembering that the delete key returns
{27, 91, 51, 126}
every time, I can simply use
DELETE_KEY
instead. But, what if I want to compile this program on windows??
All the keys will be different!!! This makes it easy to port my program to windows, or mac, if I want. With this, I also only have to change the values here, instead of at every place I use them.