#ifdef and #ifndef Directives

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

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

 

Parameters

ItemDescription

identifier

Identifier of the constant or macro to check.

 

Remarks

You can use the #ifdef and #ifndef directives anywhere that the #if can be used. The #ifdef statement is equivalent to #if 1 if the identifier is defined, and is equivalent to #if 0 if identifier is not defined or has been undefined using the #undef 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).

Examples

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.


// PROG.CPP
#ifndef test
  #define final
#endif
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, #elif, #else, and #endif Directives (DirectX HLSL)

 

 

Community Additions

ADD
Show:
© 2014 Microsoft