Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Preprocessor Syntax
- The preprocessor directives are easily distinguished from normal programming code in that they start with a hash sign (#). They must always occupy a line that is separate from anything else, except for single-line comments. Whitespace may optionally be included before and after the hash sign.
- #line 1 // set line number
- Conditional Compilation Symbols
- A conditional compilation symbol is created using the #define directive followed by the symbol’s name. When a symbol is defined, it will then cause a conditional expression using that condition to be evaluated as true. The symbol will remain defined only within the current source file, starting from the line where the symbol is created.
- #define Symbol
- The #undef (undefine) directive can disable a previously defined symbol.
- #undef Symbol
- Conditional Compilation
- The #if and #endif directives specify a section of code that will be included or excluded based on a given condition. Most often, this condition will be a conditional compilation symbol.
- #if Symbol
- // ...
- #endif
- Just as with the C# if statement, the #if directive can optionally include any number of #elif (else if) directives and one final #else directive. Conditional directives may also be nested within another conditional section. In longer conditionals, it is good practice to add comments to the #endif directives to help keep track of which #if directive they correspond to.
- #if Professional
- // ...
- #elif Advanced || Enterprise
- // ...
- #else
- #if Debug
- // ...
- #endif // Debug
- #endif // Professional
- Diagnostic Directives
- There are two diagnostic directives: #error and #warning. The #error directive is used to abort a compilation by generating a compilation error. This directive can optionally take a parameter that provides an error description.
- #if Professional && Enterprise
- #error Build cannot be both Professional and Enterprise
- #endif
- Similar to error, the #warning directive generates a compilation warning message. This directive will not stop the compilation.
- #if !Professional && !Enterprise
- #warning Build should be Professional or Enterprise
- #endif
- Line Directive
- Another directive that affects the compiler’s output is #line. This directive is used to change the line number and optionally the source filename that is displayed when an error or warning occurs during compilation. This is mainly useful when using a program that combines the source files into an intermediate file, which is then compiled.
- #line 500 "MyFile"
- #error MyError // MyError on line 500
- Region Directives
- The last two directives are #region and #endregion. They delimit a section of code that can be expanded or collapsed using the outlining feature of Visual Studio.
- #region MyRegion
- #endregion
- Just as the conditional directives, regions can be nested any number of levels deep.
- #region MyRegion
- #region MySubRegion
- #endregion
- #endregion
Add Comment
Please, Sign In to add comment