Sams Teach Yourself C++ in 21 Days

(singke) #1
#ifndef DEBUG
cout << “Debug is not defined.”;
#endif
Note that #ifndefis the logical reverse of #ifdef. #ifndefevaluates to true if the string
has not been defined up to that point in the file.
You should notice that all of these checks required that #endifalso be included to indi-
cate the end of the code impacted by the check.

The #elsePrecompiler Command ................................................................


As you might imagine, the term #elsecan be inserted between either #ifdefor #ifndef
and the closing #endif. Listing 21.1 illustrates how these terms are used.

LISTING21.1 Using #define
0: #define DemoVersion
1: #define SW_VERSION 5
2: #include <iostream>
3:
4: using std::endl;
5: using std::cout;
6:
7: int main()
8: {
9: cout << “Checking on the definitions of DemoVersion,”;
10: cout << “SW_VERSION, and WINDOWS_VERSION...” << endl;
11:
12: #ifdef DemoVersion
13: cout << “DemoVersion defined.” << endl;
14: #else
15: cout << “DemoVersion not defined.” << endl;
16: #endif
17:
18: #ifndef SW_VERSION
19: cout << “SW_VERSION not defined!” << endl;
20: #else
21: cout << “SW_VERSION defined as: “
22: << SW_VERSION << endl;
23: #endif
24:
25: #ifdef WINDOWS_VERSION
26: cout << “WINDOWS_VERSION defined!” << endl;
27: #else
28: cout << “WINDOWS_VERSION was not defined.” << endl;
29: #endif
30:
31: cout << “Done.” << endl;
32: return 0;
33: }

754 Day 21

Free download pdf