Conditional Compilation
The #if
, #ifdef
, #ifndef
, #else
, #elif
and #endif
directives can be used for conditional compilation.
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
.
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:
Translation can also be caused to fail by using the #error
directive:
Read more about this topic: C Preprocessor, Phases
Famous quotes containing the words conditional and/or compilation:
“The population of the world is a conditional population; these are not the best, but the best that could live in the existing state of soils, gases, animals, and morals: the best that could yet live; there shall be a better, please God.”
—Ralph Waldo Emerson (18031882)
“The United States Constitution has proved itself the most marvelously elastic compilation of rules of government ever written.”
—Franklin D. Roosevelt (18821945)