#define directive (constant)

Preprocessor directive that assigns a meaningful name to a constant in your application.

#define identifiertoken-string

Parameters

Item Description
identifier
Identifier of the constant.
token-string [optional]
Value of the constant. This parameter consists of a series of tokens, such as keywords, constants, or complete statements. One or more white-space characters must separate this parameter from the identifier parameter; this white space is not considered part of the substituted text, nor is any white space following the last token of the text.
If you exclude this parameter, all instances of the identifier parameter are removed from the source file. The identifier remains defined, and can be tested using the #if defined, #ifdef, and #ifndef directives.

Remarks

All instances of the identifier parameter that occur after the #define directive in the source file will be replaced by the value of the token-string parameter. The identifier is replaced only when it forms a token; for example, the identifier is not replaced if it appears in a comment, within a string, or as part of a longer identifier.

The #undef directive instructs the preprocessor to forget the definition of an identifier; for more information, see #undef Directive (DirectX HLSL).

Defining constants with the /D compiler option has the same effect as using the #define directive at the beginning of your file. Up to 30 constants can be defined with the /D option. For an example of how this can be used, see the Examples section of #ifdef and ).

Examples

The following example defines the identifier WIDTH as the integer constant 80 and then defines LENGTH in terms of WIDTH and the integer constant 10.

#define WIDTH       80
#define LENGTH      ( WIDTH + 10 )

Every subsequent instance of LENGTH is replaced by (WIDTH + 10), and every subsequent instance of WIDTH + 10 is replaced by the expression (80 + 10). The parentheses around WIDTH + 10 are important because they control the interpretation in statements such as the following.

var = LENGTH * 20;

After the preprocessing stage the statement becomes the following, which evaluates to 1,800.

var = ( 80 + 10 ) * 20;

Without parentheses, the result would be the following, which evaluates to 280.

var = 80 + 10 * 20;

Preprocessor Directives (DirectX HLSL)

#define Overloads

#undef Directive (DirectX HLSL)