Preprocessors: #define



The #define, called a directive, is used to direct the compiler to create or perform a (small) action. This action is called a macro. For example, you can ask the compiler to use "Rio de Janeiro" whenever it sees RDJ. To do that you can write

#define RDJ "Rio de Janeiro"

If you use the word RDJ in your program, the compiler would replace it with the defined name. You can also use the #define directive to create words that would be replaced with numeric values. Here is an example:

#include <iostream>

using namespace std;
using namespace System;

#define RDJ "Rio de Janeiro"
#define Print(Sentence) Console::WriteLine(Sentence)

int main()
	cout << "City: " << RDJ << "\n";
	Print("Welcome to the Wonderful World of C++/CLI.");

	return 0;

This would produce:

City: Rio de Janeiro
Welcome to the Wonderful World of C++/CLI.
Press any key to continue . . .



Home Copyright © 2006-2016, FunctionX, Inc.