Broad Network


The ifdef and ifndef Preprocessing Directives

C++ Preprocessing Directives – Part 4

Forward: In this part of the series, I talk about the #ifdef and #ifndef preprocessing directives and their use.

By: Chrysanthus Date Published: 18 Sep 2012

Introduction

This is part 4 of my series, C++ Preprocessing Directives. In this part of the series, I talk about the #ifdef and #ifndef preprocessing directives and their use. You must be reading this series in the order in which the tutorials have been given. #ifdef and #ifndef are preprocessing directives. You begin any preprocessing directive in a new line and end it by pressing the keyboard Enter key.

Illustration
In the previous part of the series, you saw the following line:

#if VERSION = = 1

This means if VERSION is equal to 1 (e.i. if the VERSION object holds 1). In the previous part of the series, VERSION was previously defined with the #defined preprocessing directive to have the value, 2. The condition here tests if VERSION is equal to 1, it does not test if VERSION has been defined, in the first place. If you want to test if VERSION has been defined at all, you would type:

#ifdef VERSION

which literally means "if define VERSION", interpreted as, if VERSION has been defined at all, in front (up) in the program.

The #ifdef Preprocessing Directive
If you want to test if an identifier has been defined with the #define preprocessing directive, the syntax is:

#ifdef Identifier

Do not forget to press the Enter key (newline character) at the end of the line. This line is followed by the block that will be executed, if the identifier has been defined up in the program.

A #ifdef Example
The following code segment illustrates the use of the #ifdef directive to change a version number:


#define VERSION 2

#ifdef VERSION
#undef VERSION
#define VERSION 3
#endif


The #ifdef construct begins with the test if VERSION was defined in the first place. Its block delimited by #ifdef and #endif has two preprocessing directives. The first directive undefines VERSION, making it non-existent, however making the word, VERSION free to be re-used in a new definition. The new definition is the second directive in the block.

Different Constructs of the #ifdef Directive
One way in which the #ifdef construct can be coded is:

#ifdef Identifier
//block of preprocessing lines
#endif


Another way is:


#ifdef Identifier
//block of preprocessing lines
#else
//block of preprocessing lines
#endif

Note how delimiting takes place. Each of the two preprocessing constructs ends with #endif. Be aware that the #ifdef construct can also use the #else preprocessing directive.

The #ifndef Directive
Just as,

    if (aaa != bbb)

is the opposite of,

    if (aaa == bbb)

so too, #ifndef is the opposite of #ifdef.

#ifndef VERSION

for example, literal means "if Not define VERSION", interpreted as, if VERSION has not been defined at all, in front (up) in the program.

Different Constructs of the #ifndef Directive
One way in which the #ifndef construct can be coded is:

#ifndef Identifier
//block of preprocessing lines
#endif


Another way is:


#ifndef Identifier
//block of preprocessing lines
#else
//block of preprocessing lines
#endif

Note how delimiting takes place. Each of the two preprocessing constructs ends with #endif. Be aware that the #ifndef construct can also use the #else preprocessing directive.

Use of the #ifdef and #ifndef
A large C++ program is written by several people. The files written by the different people are joined by include header files. I will explain the professional way of joining files in C++ later. For now I will emphasis on the use of #ifdef and #ifndef.

One programmer may define an object using a particular identifier, with the #define directive. At the end of the day, all the files are joined using the #include preprocessing directive. You cannot use an identifier to define an object that has already been used in a file that is included into the file you write. So, in your own file, you may not know whetther an identifier has been used to define an object. In this case, you have to check, if the identifier has been used in the included file. You can only make a definition in your own file if the identifier has not been used to make a definition in the included file. You have to do the checking in code, in your own file. The checking syntax is:

#ifdef Identifier
#define newIdentifier
#else
#define Identifier
#endif

You should interpret the syntax as follows: If the identifier has been defined then define a new identifier, else define the identifier with the name of interest. An example is:

#ifdef VERSION
#define EDITION 3
#else
#define VERSION 3
#endif

This example takes the version to EDITION 3 if the previous style name was "VERSION"; otherwise it takes it to VERSION 3 if neither VERSION nor EDITION (maybe something like, BUILD 2) had been defined.

You can still use #ifndef in the opposite sense.

When files are included, they form a long program. The definition with the same identifier is not allowed more than once, in a program, whether long or short. So you will be using code segments like the one just shown in your own programs.

Remember, identifiers of this nature are of macros. So #ifdef and #ifndef are generally used with macros. Note that any #ifdef or #ifndef construct ends with #endif.

That is it for this part of the series. We stop here and continue in the next part.

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