Introduction
Preprocessor Directives are commands known by the compiler. Preprocessor Directives are used to control the flow of execution in a program. Preprocessing directives are at the top of our program and start with “#”. Here “#” is the identifier for Preprocessor Directives.
Preprocessor Directives are nam “preprocessor” because Preprocessor Directives provide instructions to the compiler to preprocess the information before actual the compilation starts.
The C# compiler does not have a separate preprocessor. Unlike C and C++ directives, they are not used to create macros. A Preprocessor Directive must be the only instruction of a single line.
List of Preprocessor Directives:
Preprocessor Directives |
Meaning /Use of Preprocessor Directives |
#define |
Define a new symbol (sequences of characters) |
#undef |
Remove the predefined symbol |
#if |
Checks the symbol for a specific condtion |
#else |
Checks the symbol for a condition and if the condition is false then it executes the #else statement(s) |
#elif |
Creates compound conditional directives |
#endif |
Ends conditional directives |
#line |
Changes the line and file name in the error |
#warning |
Generates a user0defined warning |
#error |
Generates a user-defined error |
#region |
Defines a set of commands as a single block |
#endregion |
Defines the end of a region |
#pragma |
Provides compiler options for the compilation of the file under consideration |
The following is an example:
- #define nam
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace ConsoleApplication3
- {
- class Demo
- {
- static void Main(string[] args)
- {
- #if (nam) Console.WriteLine("Nam is found");#
- else Console.WriteLine("Nam not found");#endif
- Console.ReadLine();
- }
- }
- }
In the preceding example we defined a symbol named “Nam” before all the namespaces and programs. We must be define the symbol at the top otherwise it will throw an error.
In our example part of the #else region looks different because the compiler knows that an #else region will never execute because the symbol nam already exists so the compiler treats it as a comment.
Now for explanations of each Preprocessor Directive.
#define
The #define Preprocessor Directive creates symbols. This symbol may be any combination of characters.
- #define name
- #define name_
- #define _nams6
The symbol may start with an underscore or a character.
- #define name#define name_#define _nams6
- using System;
- namespace ConsoleApplication1
- {
- class program
- {
- static void Main(string[] args)
- {
- #if (name)
- Console.WriteLine("Name symbol");
- #else
- Console.WriteLine("Name symbol ot present");#endif
-
- #if (pankaj)
- Console.WriteLine("pankaj symbol");
- #else
- Console.WriteLine("pankaj symbol ot present");#endif
- Console.ReadKey();
- }
-
- }
- }
Output
#Undef
The Undef Preprocessor Directive undefines (removes) the predefined symbol.
- #define name#define name_#undef name
- namespace ConsoleApplication1
- {
- class program
- {
- static void Main(string[] args)
- {
- #if (name)
- Console.WriteLine("Name symbol");
- #else
- Console.WriteLine("Name symbol ot present");
- #endif
- #if (pankaj)
- Console.WriteLine("pankaj symbol");
- #else
- Console.WriteLine("pankaj symbol ot present");#endif
- Console.ReadKey();
- }
-
- }
- }
Output
#if , #else, # elif , #endif
The preceding Preprocessor Directives allow you to test whether or not a specific symbol has been defined. Based on the outcome of this test you can conditionally compile a piece of code.
Mainly the #define Preprocessor Directive is used with #if, #elif and #else Preprocessor Directives.
- #define name#define name_#undef name#define _nams6
- using System;
- namespace ConsoleApplication1
- {
- class program
- {
- static void Main(string[] args)
- {
- #if (name)
- Console.WriteLine("Name symbol Found");
- #elif(name_)
- Console.WriteLine("Name_ symbol Found");
- #else
- Console.WriteLine("Noting Found");#endif
- Console.ReadKey();
- }
- }
- }
Output
Conditional Directives
You also use the Preprocessor Directive with conditional operators. Using conditional operators with Preprocessor Directives you can handle the execution of your program very well and also generate more flexibility in the program.
You can use the following conditional operators.
- == (equality)
- != (inequality)
- && (and)
- || (or)
The following is an example:
- #define name#define name_#define _nams6
- using System;
- namespace ConsoleApplication1
- {
- class program
- {
- static void Main(string[] args)
- {
- #if (name && !name_)
- Console.WriteLine("name is defined");
- #elif(!name && name_)
- Console.WriteLine("name_ is defined");
- #elif(name && name_)
- Console.WriteLine("name and name_ are defined");
- #else
- Console.WriteLine("name and name_ are not defined");
- #endif
- Console.ReadKey();
- }
-
- }
- }
Output
#warning
The Warning Preprocessor Directive generates a warning at a specific location. You can use a warning directive with other Preprocessor Directives like #if, #elif and #else. You can also generate conditional warnings using the conditional operator.
- #define name#define name_#undef name#undef name_#define _nams6
- using System;
- namespace ConsoleApplication1
- {
- class program
- {
- static void Main(string[] args)
- {
- #if (name && !name_)
- Console.WriteLine("name is defined");
- #elif(!name && name_)
- Console.WriteLine("name_ is defined");
- #elif(name && name_)
- Console.WriteLine("name and name_ are defined");
- #else
- #warning "Nothing is found,Please use a preprocssor directive"
- #endif
- Console.ReadKey();
- }
- }
- }
Output
#error
The #error Preprocessor Directive generates an error in the program. The #warning and #error directives are the same except that an #error directive stops the compilation of the program but the #warning never stops the of the program, it only generates a warning.
- #define name#define name_#undef name#undef name_#define _nams6
- using System;
- namespace ConsoleApplication1
- {
- class program
- {
- static void Main(string[] args)
- {
- #if (name && !name_)
- Console.WriteLine("name is defined");
- #elif(!name && name_)
- Console.WriteLine("name_ is defined");
- #elif(name && name_)
- Console.WriteLine("name and name_ are defined");
- #else
- #warning "warning directive never stop execution of program"#error "error directive stop the execution of program"
- #endif
- Console.ReadKey();
- }
- }
- }
Output
#region and # endregion
The #region Preprocessor Directive defines a set of commands as a single block such that the entire block of commands can execute in a single run. The #endregion Preprocessor Directive ends the region.
- #define name#define name_
- using System;
- namespace ConsoleApplication1
- {
- class program
- {
- static void Main(string[] args)
- {
- int A, B, C;
- A = 10;
- B = 20;
- #region(name && name_)
- A = 100;
- B = 200;
- #endregion
- C = A + B;
- Console.WriteLine("Total is {0}", C.ToString());
- Console.ReadKey();
- }
- }
- }
Output
#line
The #line Preprocessor Directive modifies the line number and file name that appears in the compiler error messages. The #line directive is more useful in situations where the source code is modified by some external tool or other source.
- class program
- {
- static void Main(string[] args)
- {
- int A, B, C;
- A = 10;
- B = 20;
- C = A + * B;
- Console.WriteLine("Output is {0}", C.ToString());
- Console.ReadKey();
- }
- }
Output
Now we use the #line Preprocessor Directive to change the line number and file name in the error.
- class program
- {
- static void Main(string[] args)
- {
- int A, B, C;
- A = 10;
- B = 20;
- #line 250 "OtherFile.cs"
- C = A + * B;
- Console.WriteLine("Output is {0}", C.ToString());
- Console.ReadKey();
- }
- }
Output
We changed the line number and file name in an error.
#pragma
The #pragma Preprocessor Directive instructs the compiler about the compilation of the file under consideration. The instructions in a #pragma must be supported by the compiler.
#pragma mainly uses the following two instructions.
- #pragma warning
- #pragma checksum
#pragma warning
- #pragma warning is used to disable all warnings or a specific warning in the program. You can use:
- #pragma warning disable: Disable all warningd
- #pragma warning restore : Enables all warningd
- #pragma warning disable warning-list: Disable a specific warning
The following is an example:
- class program
- {
- static void Main(string[] args)
- {
- int A, B, C, d, m, n;
- string str;
- A = 10;
- B = 20;
- Console.ReadKey();
- }
- }
Output
Now we use a #pragma warning disable directive to remove warnings:
In the preceding example the compiler is not showing any warnings after using the #pragma warning disable.
When to use Preprocess Directives
- To perform conditional compilation.
- To remove warnings from the program
- To show an error or warning at a specific line under some condition(s).
- To change the line number of a file name in an error.
- When it is necessary to apply a region (execute a set of commands under certain conditions)