#ifdef and #ifndef Directives

Preprocessor directives that determine whether a specific preprocessor constant or macro is defined.

#ifdef identifier ...
#ifndef identifier ...


Item Description
Identifier of the constant or macro to check.


You can use the #ifdef and #ifndef directives anywhere that the #if can be used. The #ifdef statement is equivalent to ) directive. These directives check only for the presence or absence of identifiers defined using the #define directive, not for identifiers declared in the C or C++ source code.

These directives are provided only for compatibility with previous versions of the language. The use of the defined operator with the #if directive is preferred.

The #ifndef directive checks for the opposite of the condition checked by #ifdef. If the identifier is not defined, the condition is true (nonzero); otherwise, the condition is false (zero).


The identifier can be passed from the command line using the /D option. Up to 30 macros can be specified with /D. This is useful for checking whether a definition exists, because a definition can be passed from the command line. The following example uses this behavior to determine whether to run an application in test mode.

#ifndef test
  #define final
int main()

When compiled using the following command, prog.cpp will compile in test mode; otherwise, it will compile in final mode.

CL.EXE /Dtest prog.cpp

See also

Preprocessor Directives (DirectX HLSL)

#if, )