Basics of C++ Define Preprocessing Directive

C++ Taking the Bull by the Horns - Part 19

Chrys Forcha
Introduction
This is part 19 of my series, C++ Tutorials from Roots. You have what is called, Preprocessor Directives. One of then is called the define directive. In this part of the series, we look at the basics of the C++ define Preprocessing Directive.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Constant
When dealing with a pointer, you have two objects. So you can talk about a constant pointer (the address in the pointer object is constant), or you can talk about the constant value (content) of the pointed object. When dealing with only one object, you can talk about making its value constant. The define preprocessing directive is another way of keeping the value of a single object constant, but you should do its coding at the top of the source code file.

Consider the following statement:

const int myIdent = 25;

You can do the same thing with the preprocessing directive at the top of your source file, as follows:

#define myIdent 25

The define preprocessing directive begins with the # symbol at the beginning of a line. This is followed by the word define. After this you have a space, then the identifier you want for the object. Then a space and then the value for the object. All of that should be in one line. Anything you type next should go to the next line. This means that after typing the define preprocessing line, you have to press the Enter Key. This value will remain constant. Note that there is no object type indicator. Also note that the directive does not end with a semicolon.

The type of value can be an int, float, _Bool, char or string. There is more to the define preprocessing directive. However, for this basic tutorial let us not worry about the extra things. If it is a char, it has to be in single quotes. If it is a string, it has to be in double quotes. Read and try the following code for the case of an int:

#include
using namespace std;

#define myIdent 25

int main()
{
cout
return 0;
}

Actually the first two lines (without semicolons) in the above code are preprocessor directives, but we shall talk about the very first one later. The third line is our preprocessor directive of interest. Note that even as the object type is int, there is no object type indicator in the preprocessing line. Also note that the preprocessing directive is in one line and there is no semicolon at the end of the line.

You do not have to specify the type in the define preprocessing directive.

The above define preprocessing directive is like an initialization statement, but remember, it makes the value of the object constant. You can still have the define preprocessing directive looking like declaration (and then assignment down in the code). However, to assign and use the value, you have to play around with the identifier as illustrated in the following code. Read and try it.

#include
using namespace std;

#define myIdent

int main()
{

char myChar = myIdent 'A';

cout
return 0;
}

The define preprocessing directive above is "#define myIdent" without the value. In the main function you have the statement:

char myChar = myIdent 'A';

The right operand begins with the identifier of the preprocessing directive. This is followed by a space and then the assigned value. You can type any value there. I wanted a char, so I typed A in single quotes. All what you have in that line is a statement, not a preprocessor directive, so it ends with a semicolon. In the left operand, the value A as a char is assigned to the object identified by, myChar in the statement. That is how you can do a kind of assignment for the identifier of the preprocessing directive, e.g. myIdent. Now that at the preprocessing directive, at the top of the source file, the value was not typed, the cout object used but the myChar identifier and not myIdent.

String and the define Preprocessor Directive
The following code illustrates how to use the string as constant value to the identifier of the preprocessing directive:

#include
using namespace std;

#define myIdent "The only Place"

int main()
{

cout
return 0;
}

Read and try the above code if you have not already done so.

The undef Preprocessing Directive
When you use the define preprocessing directive, you make the value of the object for the identifier of the directive, constant. After the define directive, you can undo it. You use the undef preprocessing directive for this and the whole define directive will be effectively cancelled. The following code shows how the undef directive is used. Read and try the code and note that the compiler will not compile, and will issue error message, because the undef directive has cancelled the defined directive:

#include
using namespace std;

#define myIdent 25
#undef myIdent

int main()
{

cout
return 0;
}

Note, all preprocessing directive begins with #. After undefining, you can still use the same identifier to redefine, another value. Read and try the following code:

#include
using namespace std;

#define myIdent 25
#undef myIdent
#define myIdent 98

int main()
{

cout
return 0;
}

Let us end here for this basic tutorial. You will need to learn more about the #define directive. I will not cover the extra bits in this series. We continue in the next part of the series.

Chrys

To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search (you can also use any available links):

C++ Taking the Bull by the Horns - Part 1
C++ Taking the Bull by the Horns - Part 2
C++ Taking the Bull by the Horns - Part 3
C++ Taking the Bull by the Horns - Part 4
C++ Taking the Bull by the Horns - Part 5
C++ Taking the Bull by the Horns - Part 6
C++ Taking the Bull by the Horns - Part 7
C++ Taking the Bull by the Horns - Part 8
C++ Taking the Bull by the Horns - Part 9
C++ Taking the Bull by the Horns - Part 10
C++ Taking the Bull by the Horns - Part 11
C++ Taking the Bull by the Horns - Part 12
C++ Taking the Bull by the Horns - Part 13
C++ Taking the Bull by the Horns - Part 14
C++ Taking the Bull by the Horns - Part 15
C++ Taking the Bull by the Horns - Part 16
C++ Taking the Bull by the Horns - Part 17
C++ Taking the Bull by the Horns - Part 18
C++ Taking the Bull by the Horns - Part 19
C++ Taking the Bull by the Horns - Part 20
C++ Taking the Bull by the Horns - Part 21
C++ Taking the Bull by the Horns - Part 22
C++ Taking the Bull by the Horns - Part 23
C++ Taking the Bull by the Horns - Part 24
C++ Taking the Bull by the Horns - Part 25

Published by Chrys Forcha

I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.