/D (Preprocessor Definitions)

Defines a preprocessing symbol for a source file.

/Dname[= | # [{string | number}] ]

Remarks

You can use this symbol with #if or #ifdef to compile source conditionally. The symbol definition remains in effect until a redefinition is encountered in source or the symbol is undefined in source by using the #undef directive.

/D has much the same effect as using the #define directive at the beginning of the source file. However, /D strips quotes on the command line and #define retains them.

By default, the value associated with a symbol is 1. That is, /DTEST is equivalent to /DTEST=1. In the following example, the definition of TEST is shown to print 1.

Compiling by using /Dname= causes the symbol to not have an associated value. While the symbol can still be used to conditionally compile code, the symbol otherwise evaluates to nothing. For example, in the sample program, compiling by using /DTEST= causes a compiler error. This behavior resembles using #define with or without a value.

The following command defines the symbol DEBUG in TEST.c.

CL /DDEBUG  TEST.C

The following command removes all occurrences of the keyword __far in TEST.c.

CL /D__far=  TEST.C

You cannot set the CL environment variable to a string that contains an equal sign. To use /D with the CL environment variable, you must specify a number sign instead of an equal sign.

SET CL=/DTEST#0

When you define a preprocessing symbol at the command prompt, consider shell parsing rules as well as compiler parsing rules. For example, to define one percent-sign preprocessing symbol (%) in your program, specify two percent-sign characters (%%) on the command line. If you specify only one percent-sign, a parsing error is emitted.

CL /DTEST=%% TEST.C

To set this compiler option in the Visual Studio development environment

  1. Open the project Property Pages dialog box. For more information, see How to: Open Project Property Pages.

  2. In the left pane, click C/C++ and then click Preprocessor.

  3. In the right pane, modify the Preprocessor Definitions property.

To set this compiler option programmatically

Example

// cpp_D_compiler_option.cpp
// compile with: /DTEST
#include <stdio.h>

int main( )
{
    #ifdef TEST
        printf_s("TEST defined %d\n", TEST);
    #else
        printf_s("TEST not defined\n");
    #endif
}
TEST defined 1

See Also

Reference

Compiler Options

Setting Compiler Options

/U, /u (Undefine Symbols)

#undef Directive (C/C++)

#define Directive (C/C++)