161 Contributors: 7 Monday, October 31, 2016
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial:
Roadmap: roadmap

Preprocessor directives


  • #define [symbol] // Defines a compiler symbol.
  • #undef [symbol] // Undefines a compiler symbol.
  • #warning [warning message] // Generates a compiler warning. Useful with #if.
  • #error [error message] // Generates a compiler error. Useful with #if.
  • #line [line number] (file name) // Overrides the compiler line number (and optionally source file name). Used with T4 text templates.
  • #pragma warning [disable|restore] [warning numbers] // Disables/restores compiler warnings.
  • #pragma checksum "[filename]" "[guid]" "[checksum]" // Validates a source file's contents.
  • #region [region name] // Defines a collapsible code region.
  • #endregion // Ends a code region block.
  • #if [condition] // Executes the code below if the condition is true.
  • #else // Used after an #if.
  • #elif [condition] // Used after an #if.
  • #endif // Ends a conditional block started with #if.


Preprocessor directives are typically used to make source programs easy to change and easy to compile in different execution environments. Directives in the source file tell the preprocessor to perform specific actions. For example, the preprocessor can replace tokens in the text, insert the contents of other files into the source file, or suppress compilation of part of the file by removing sections of text. Preprocessor lines are recognized and carried out before macro expansion. Therefore, if a macro expands into something that looks like a preprocessor command, that command is not recognized by the preprocessor.

Preprocessor statements use the same character set as source file statements, with the exception that escape sequences are not supported. The character set used in preprocessor statements is the same as the execution character set. The preprocessor also recognizes negative character values.

Conditional Expressions

Conditional expressions (#if, #elif, etc) do support a limited subset of boolean operators. They are:

  • == and !=. These can only be used for testing whether the symbol is true (defined) or false (not defined)
  • &&, ||, !
  • ()

For example:


would compile code that prints "OK!" to the console if DEBUG is not defined, either SOME_SYMBOL or SOME_OTHER_SYMBOL is defined, and RELEASE is defined.

Note: These substitutions are done at compile time and are therefore not available for inspection at run time. Code eliminated through use of #if is not part of the compiler's output.

See Also: C# Preprocessor Directives at MSDN.

Related Examples