The #if, #ifdef, #elif...defined, #undef, #endif, #define, #if...defined preprocessor directives C++ program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: Doing the conditional compilation for C/C++ program debugging and testing using #if, #ifdef, #elif...defined, #undef, #endif, #define, #if...defined preprocessor directives

To show: The #if, #ifdef, #elif...defined, #undef, #endif, #define, #if...defined preprocessor directives usage for conditional compilation

 

// The preprocessing directives #define and #undef allow the definition of identifiers which hold a certain value.

// These identifiers can simply be constants or a (macro) function. The directives #ifdef and #ifndef allow conditional compilation of certain

// lines of code based on whether or not an identifier has been defined.

#define ADDNUM

#define SUBTRACTNUM

#define DIVIDENUM

#define MULTIPLYNUM

 

#include <iostream>

using namespace std;

 

// global variables

float p, q;

 

// function prototypes

float AddNum(float, float);

float SubtractNum(float, float);

float DivideNum(float, float);

float MultiplyNum(float, float);

int printerror();

 

void main(void)

{

// local scope (to this program/file) variables

int r = 0, s= 0, u= 0;

float t= 0;

 

cout<<"Enter two numbers separated by space: "<<endl;

cin>>p>>q;

 

// The compiler only compiles the code after the #if expression

// if the constant_expression evaluates to a non-zero value (true).

// If the value is 0 (false), then the compiler skips the lines until

// the next #else, #elif, or #endif. If there is a matching #else, and

// the constant_expression evaluated to 0 (false), then the lines between

// the #else and the #endif are compiled. If there is a matching #elif, and the

// preceding #if evaluated to false, then the constant_expression after that is

// evaluated and the code between the #elif and the #endif is compiled only if this

// expression evaluates to a non-zero value (true)

#ifdef ADDNUM // #if defined ADDNUM or #if defined(ADDNUM)

r = (int)AddNum(p, q);

#endif

 

#ifdef SUBTRACTNUM

s = (int)SubtractNum(p, q);

// #elif defined ...

#endif

 

#ifdef DIVIDENUM

// Not a good float to int conversion/type casting, lost precision for double/float

t = DivideNum(p, q);

#endif

 

#if 1 // another variation: 0 - FALSE, other than 0 - TRUE

u = (int) MultiplyNum(p, q);

#else

printerror();

#endif

 

cout<<"Addition: "<<p <<" + "<<q<<" = "<<r<<endl;

cout<<"Subtraction: "<<p <<" - "<<q<<" = "<<s<<endl;

cout<<"Division: "<<p <<" / "<<q<<" = "<<t<<endl;

cout<<"Multiplication: "<<p <<" * "<<q<<" = "<<u<<endl;

}

 

float AddNum(float x, float y)

{

return (x + y);

}

 

float SubtractNum(float x, float y)

{

return (x - y);

}

 

float DivideNum(float x, float y)

{

// divide by 0 check

if(y==0)

{

printf("========Divide by 0 error!!!\n");

return 0;

}

else

return (x / y);

}

 

float MultiplyNum(float x, float y)

{

return (x * y);

}

 

int printerror()

{

printf("Error encountered....\n");

return 1;

}

 

Output examples:

 

Enter two numbers separated by space:

7 3

Addition: 7 + 3 = 10

Subtraction: 7 - 3 = 4

Division: 7 / 3 = 2.33333

Multiplication: 7 * 3 = 21

Press any key to continue . . .

 

 

Comment out the #define SUBTRACTNUM, rebuild and re-run the program

 

Enter two numbers separated by space:

7 3

Addition: 7 + 3 = 10

Subtraction: 7 - 3 = 0

Division: 7 / 3 = 2.33333

Multiplication: 7 * 3 = 21

Press any key to continue . . .

 

Change the #if 1 to #if 0 for multiplication, rebuild and re-run the program

 

Enter two numbers separated by space:

7 3

Error encountered....

Addition: 7 + 3 = 10

Subtraction: 7 - 3 = 0

Division: 7 / 3 = 2.33333

Multiplication: 7 * 3 = 0

Press any key to continue . . .

 

Add the following #ifdef...#undef...#endif after #define MULTIPLYNUM, rebuild and re-run the program

 

...

#ifdef ADDNUM

#undef ADDNUM

#endif

...

 

 

Enter two numbers separated by space:

7 3

Error encountered....

Addition: 7 + 3 = 0

Subtraction: 7 - 3 = 0

Division: 7 / 3 = 2.33333

Multiplication: 7 * 3 = 0

Press any key to continue . . .

 

Take note that the program rebuild and re-run successfully. These preprocessor directives for conditional compilation are very useful for program debugging.

 

 

C and C++ Programming Resources | C & C++ Code Example Index