Broad Network


Preprocessing Conditionals

C++ Preprocessing Directives – Part 3

Forward: In this part of the series, I talk about C++ preprocessing conditionals.

By: Chrysanthus Date Published: 18 Sep 2012

Introduction

This is part 3 of my series, C++ Preprocessing Directives. In this part of the series, I talk about C++ preprocessing conditionals.You must be reading the tutorials of this series in the order given.

What You should already know on Conditionals
You already know the conditional construct:

    if (condition)
        {
         //statements
        }

You also already know the conditional construct:

    if (condition)
        {
         //statements
        }
    else
        {
            //statements
        }

You already know as well the conditional construct:

    if (condition)
        {
         //statements
        }
    else if
     {
            //statements
        }
    else if
     {
            //statements
        }

    - - -

    else
        {
            //statements
        }

Preprocessing Equivalent Conditionals
The preprocessor has equivalents to the above normal conditionals:

The following preprocessor construct is similar to the first construct above:

#if conditionWithDefinedIdentifier
//block of preprocessing lines
#endif

Here, the condition is not surrounded by parentheses. The block is typically made up of preprocessing lines and not statements. A preprocessing line does not end with a semicolon. You can still have a statement within the block. Any statement present will end with a semicolon. The block is not delimited with curly brackets. It is delimited by the preprocessing directives, #if and #endif. So, #endif ends the block.

The following preprocessor construct is similar to the second construct above:

#if conditionWithDefinedIdentifier
//block of preprocessing lines
#else
//block of preprocessing lines
#endif

In this case only one of the blocks of preprocessing lines is executed. The first block is delimited by #if and #else and the second block is delimited by #else and #endif.

The following preprocessor construct is similar to the third construct above:

#if conditionWithDefinedIdentifier
//block of preprocessing lines
#elif conditionWithDefinedIdentifier
//block of preprocessing lines
#elif conditionWithDefinedIdentifier
//block of preprocessing lines
---
#else
//block of preprocessing lines
#endif

Note how delimiting takes place. Also note that with the preprocessing directives, you have "elif" and not "else if". Each of the three preprocessing constructs ends with #endif.

Conditional Inclusion Example
The following is an example of the use of preprocessing conditionals. The example is taken from the specification and modified.


#define VERSION 2

#if VERSION = = 1
#define INCFILE "vers1.h"
#elif VERSION = = 2
#define INCFILE "vers2.h" /* and so on - the other elifs*/
#else
#define INCFILE "versN.h"
#endif

#include INCFILE


The identifier, VERSION is defined to have the value of 2. The first condition, tests if the value is 1; the second tests if the value is 2; and the default corresponding to #else does not test anything. Each block has just one preprocessing line. In the whole code segment, there is no semicolon, as there is no statement. The whole code segment consists of preprocessing lines.

In the whole construct, only one block is executed. So, the INCFILE identifier is defined only once. In this situation, there is no need to use the #undef directive as INCFILE is defined only once. Also remember, as you learned in the first part of this series, the #define preprocessing directive can be used to give the value of a string to an identifier. That is the case of each of the blocks in the above code segment.

The very last statement actually includes a file using the defined identifier, INCFILE. The actual file included depends on the result of the preprocessing conditional construct. The value of the identifier, INCFILE is the name of a file.

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