C++/Development/macro

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Printing values in DEBUG mode.

<source lang="cpp">

  1. include <iostream>

using namespace std;

  1. define DEBUG
  2. ifndef DEBUG
  3. define PRINT(x)
  4. else
  5. define PRINT(x) \
  cout << #x << ":\t" << x << endl;
  1. endif

enum BOOL { FALSE, TRUE } ; int main() {

  int x = 5;
  long y = 73898l;
  PRINT(x);
  for (int i = 0; i < x; i++)
  {
     PRINT(i);
  }
  PRINT (y);
  PRINT("Hi.");
  int *px = &x;
  PRINT(px);
  PRINT (*px);
  return 0;

}


 </source>


Using #define.

<source lang="cpp">

  1. define DemoVersion
  2. define DOS_VERSION 5
  3. include <iostream>

using namespace std; int main() { cout << "Checking on the definitions of DemoVersion, DOS_VERSION and WINDOWS_VERSION...\n";

  1. ifdef DemoVersion
  cout << "DemoVersion defined.\n";
  1. else
  cout << "DemoVersion not defined.\n";
  1. endif
  2. ifndef DOS_VERSION
  cout << "DOS_VERSION not defined!\n";
  1. else
  cout << "DOS_VERSION defined as: " << DOS_VERSION << endl;
  1. endif
  2. ifdef WINDOWS_VERSION
  cout << "WINDOWS_VERSION defined!\n";
  1. else
  cout << "WINDOWS_VERSION was not defined.\n";
  1. endif
cout << "Done.\n";
return 0;

}


 </source>