#define name

Defines the name as a condition for later including or excluding lines (see #ifdef), or for setting other special conditions during compilation.

Example:

#define TEST
...
#ifdef TEST
printf("This is a test!");
#endif

#define name value

Every time the name appears in the script below the #define, it will be replaced by the value, which can be another name, a number, or a simple arithmetic expression. Replacing names makes functions more 'readable', for instance by giving the entities' general purpose skill parameters some meaningful names.

Examples:

#define PI 3.14159
#define HEALTH skill17
#define WINAPI __stdcall
...
x = 2.0*PI;
my.HEALTH -= 50;

long WINAPI MessageBox(HWND,char *,char *,long);

Remarks

#undef name

Undefines a previously defined name.

#define macro(parameter,..)  expression(parameter,..)

Defines a macro as a replacement or abbreviation for a numerical expression. Whenever the macro is encountered in the code, the expression is executed. Macros work rather like functions, but with some minor differences. Since macros are implemented as a textual substitution, there is no effect on program performance (as with functions), however they produce larger code than functions. They are normally only used for fairly small expressions.

Examples:

#define set(obj,flag) obj.flags |= (flag)
#define reset(obj,flag) obj.flags &= ~(flag)
#define toggle(obj,flag) obj.flags ^= (flag)
#define is(obj,flag) (obj.flags & (flag))
#define zero(ptr) memset((void*)&ptr,0,sizeof(ptr))

#define macro(parameter,..)  expression(parameter##token,..)

The merging operator ## adds the token to the parameter. Useful for redefining variable or functions names in a macro.

Example:

#define merge3(name) merge(name##1,name##2,name##3) // merge3(test) is evaluated to merge(test1,test2,test3) 

Remarks

 

Some special #defines:

#define PRAGMA_ZERO

Initializes all local variables to 0.

#define PRAGMA_API  FunctionName;ModuleName!ProcName

Loads the function prototype FunctionName from function ProcName in the DLL ModuleName (see Using DLLs). Example:
#define PRAGMA_API MessageBox;user32!MessageBoxA

See also:

#ifdef, #ifndef, #else, #endif

► latest version online