C Preprocessor - Phases - Conditional Compilation

Conditional Compilation

The #if, #ifdef, #ifndef, #else, #elif and #endif directives can be used for conditional compilation.

#if VERBOSE >= 2 print("trace message"); #endif

Most compilers targeting Microsoft Windows implicitly define _WIN32. This allows code, including preprocessor commands, to compile only when targeting Windows systems. A few compilers define WIN32 instead. For such compilers that do not implicitly define the _WIN32 macro, it can be specified on the compiler's command line, using -D_WIN32.

#ifdef __unix__ /* __unix__ is usually defined by compilers targeting Unix systems */ # include #elif defined _WIN32 /* _Win32 is usually defined by compilers targeting 32 or 64 bit Windows systems */ # include #endif

The example code tests if a macro __unix__ is defined. If it is, the file is then included. Otherwise, it tests if a macro _WIN32 is defined instead. If it is, the file is then included.

A more complex #if example can use operators, for example something like:

#if !(defined __LP64__ || defined __LLP64__) || defined _WIN32 && !defined _WIN64 // we are compiling for a 32-bit system #else // we are compiling for a 64-bit system #endif

Translation can also be caused to fail by using the #error directive:

#if RUBY_VERSION == 190 # error 1.9.0 not supported #endif

Read more about this topic:  C Preprocessor, Phases

Famous quotes containing the words conditional and/or compilation:

    Conditional love is love that is turned off and on....Some parents only show their love after a child has done something that pleases them. “I love you, honey, for cleaning your room!” Children who think they need to earn love become people pleasers, or perfectionists. Those who are raised on conditional love never really feel loved.
    Louise Hart (20th century)

    The United States Constitution has proved itself the most marvelously elastic compilation of rules of government ever written.
    Franklin D. Roosevelt (1882–1945)