Specify #define

Can someone give me some examples of how to use #define in C #?

#define //preprocessor directive 

What is the purpose of this? Here is an example from Microsoft that I still don't get:

 // preprocessor_if.cs #define DEBUG #define MYTEST using System; public class MyClass { static void Main() { #if (DEBUG && !MYTEST) Console.WriteLine("DEBUG is defined"); #elif (!DEBUG && MYTEST) Console.WriteLine("MYTEST is defined"); #elif (DEBUG && MYTEST) Console.WriteLine("DEBUG and MYTEST are defined"); #else Console.WriteLine("DEBUG and MYTEST are not defined"); #endif } } 
+7
source share
5 answers

Quote :

The lines #define and #undef should be displayed at the very top of the source text file, and they can adjust the compilation options for the entire file.

and

In C #, the line #define is considered a preprocessing directive. There are some invalid syntaxes for specific characters; you cannot use a numeric value as a specific identifier, for example.

and

The #undef directive ensures that after a text dot in a file, the specified identifier is undefined.

In your example, if DEBUG defined (it doesn't matter if it is defined), and MYTEST is not defined, then it will show the message shown.

Otherwise, if DEBUG not defined, but MYTEST is defined, then it will show the displayed message.

If both are defined, a message is displayed.

Bottom line:

The definition point is to selectively apply compilation options to your program to give it a different program stream. This is a port from C and C ++.

+4
source

"Preprocessor" means that the results are executed before the main processing performed by the compiler. One of its main uses is to include or exclude code from a file before compiling it.

In the example you see, the preprocessor will first #define these two characters, and then evaluate the various #if/#elif operators to figure out what code should end up in the file. In this case, both characters are defined, so only a line saying so is output, and only this code will be compiled. If you want to decompile the result, you will see only the operator one Console.WriteLine() and nothing more.

+6
source

#define is a special "before compilation" directive in C # (it is based on the old CPP directive), which defines a preprocessor symbol.

In combination with #if , depending on which characters are defined, different code will be effectively commented out. Because of this, the code in unselected paths should not even be in a compiled state!

In the above example, the DEBUG and MYTEST defined manually at the top (with #define ), so the code β€œlooks” like this when it starts the β€œnormal” compilation phase: (In C #, this preprocessing is part of the compiler, not a separate tool, which starts first.)

 // preprocessor_if.cs //#define DEBUG //#define MYTEST using System; public class MyClass { static void Main() { //#if (DEBUG && !MYTEST) //Console.WriteLine("DEBUG is defined"); //#elif (!DEBUG && MYTEST) //Console.WriteLine("MYTEST is defined"); //#elif (DEBUG && MYTEST) Console.WriteLine("DEBUG and MYTEST are defined"); //#else //Console.WriteLine("DEBUG and MYTEST are not defined"); //#endif } } 

The DEBUG symbol is usually set automatically, depending on whether the project is being built for debugging and other symbols can be set as part of the project itself.

Happy coding.

+1
source

Read about preprocessor directives. I write C ++, so I do not use it the same way you do, but basically these are the things that you define before executing the statements. Typically, they can be used in two ways. 1.) To define something before the start of the first function, that is, you can define the numerical value of the pie before calling pie as a variable in your main function. 2.) Tells the program what to load in order to properly execute your program.

+1
source

a preprocessor directive is what will be evaluated at compile time. The #define directive is to declare a variable that can be evaluated at compile time.

  #if (DEBUG && !MYTEST) Console.WriteLine("DEBUG is defined"); #endif 

the above line will not be compiled because DEBUG and MYTEST are defined at the beginning of the file.

0
source

All Articles