Broad Network


Basics of C++ define Preprocessing Directive

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

Forward: In this part of the series, we look at the basics of the C++ define Preprocessing Directive.

By: Chrysanthus Date Published: 22 Aug 2012

Introduction

This is part 19 of my series, C++ Taking the Bull by the Horns. 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. The 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 <iostream>
using namespace std;

#define myIdent 25

int main()
    {
        cout << myIdent;

        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 definition (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 <iostream>
using namespace std;

#define myIdent

int main()
    {

     char myChar = myIdent 'A';

     cout << myChar;

        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 <iostream>
using namespace std;

#define myIdent "The only Place"

int main()
    {

        cout << myIdent;

        return 0;
    }

Read and try this 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 <iostream>
using namespace std;

#define myIdent 25
#undef myIdent

int main()
    {

        cout << myIdent;

        return 0;
    }

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

#include <iostream>
using namespace std;

#define myIdent 25
#undef myIdent
#define myIdent 98

int main()
    {

        cout << myIdent;

        return 0;
    }

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

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message