Preprocessor Directive

Why Trust Techopedia

What Does Preprocessor Directive Mean?

Preprocessor directives are lines included in a program that begin with the character #, which make them different from a typical source code text. They are invoked by the compiler to process some programs before compilation. Preprocessor directives change the text of the source code and the result is a new source code without these directives.

Advertisements

Although preprocessing in C# is conceptually similar to that in C/C++, it is different in two aspects. First, preprocessing in C# does not involve a separate step for preprocessor execution before compilation. It is processed as a part of the lexical analysis phase. Second, it cannot be used to create macros. In addition, the new directives #region and #unregion have been added in C# along with the exclusion of some directives used earlier (#include is a notable directive whose use is replaced with "using" to include assemblies).

Java does not support preprocessor directives.

Techopedia Explains Preprocessor Directive

A preprocessor directive is usually placed in the top of the source code in a separate line beginning with the character "#", followed by directive name and an optional white space before and after it. Because a comment on the same line of declaration of the preprocessor directive has to be used and cannot scroll through the following line, delimited comments cannot be used. A preprocessor directive statement must not end with a semicolon (;). Preprocessor directives can be defined in source code or in the common line as argument during compilation.

Examples for preprocessing directives that can be used in C# include:

  • #define and #undef: To define and undefine conditional compilation symbols, respectively. These symbols could be checked during compilation and the required section of source code can be compiled. The scope of a symbol is the file in which it is defined.
  • #if, #elif, #else, and #endif: To skip part of source code based on conditions. Conditional sections may be nested with directives forming complete sets.
  • #line: To control line numbers generated for errors and warning. This is mostly used by meta-programming tools to generate C# source code from some text input. It is generally used to modify the line numbers and source file names reported by the compiler in its output.
  • #error and #warning : To generate errors and warnings, respectively. #error is used to stop compilation, while #warning is used to continue compilation with messages in the console.
  • #region and #endregion :To explicitly mark sections of source code. These allow expansion and collapse inside Visual Studio for better readability and reference.
Advertisements

Related Terms

Margaret Rouse
Editor

Margaret jest nagradzaną technical writerką, nauczycielką i wykładowczynią. Jest znana z tego, że potrafi w prostych słowach pzybliżyć złożone pojęcia techniczne słuchaczom ze świata biznesu. Od dwudziestu lat jej definicje pojęć z dziedziny IT są publikowane przez Que w encyklopedii terminów technologicznych, a także cytowane w artykułach ukazujących się w New York Times, w magazynie Time, USA Today, ZDNet, a także w magazynach PC i Discovery. Margaret dołączyła do zespołu Techopedii w roku 2011. Margaret lubi pomagać znaleźć wspólny język specjalistom ze świata biznesu i IT. W swojej pracy, jak sama mówi, buduje mosty między tymi dwiema domenami, w ten…