C # pre-compiled commands with essential knowledge

Source: Internet
Author: User
C # pre-compiled commands with essential knowledge Author: Alen Source: IT expert network forum

Pre-compilation is generally used to prevent repeated inclusion and compilation of header files. The pre-compilation command indicates the operations performed by the compiler before the program is officially compiled, and can be placed anywhere in the program. This article briefly introduces the pre-compiled commands in C.

 

Pre-compilation, also known as preprocessing, is the replacement of some code text. Processing commands starting with #, such as copying the file code contained in # include, # replacement of the define macro definition, and Conditional compilation, is the stage of preparation for compilation, main Processing # pre-compiled commands.

The following describes the pre-compiled commands in C:

  1. # define and # UNDEF

Usage:

# Define debug

# UNDEF debug

# Define tells the compiler that I define a debug symbol. It is similar to a variable, but it does not have a specific value. It can be regarded as a symbol. # UNDEF is the definition of this symbol. If the debug symbol is not defined, # UNDEF does not work; otherwise, # define does not work. Both must be placed before the source code. The order of the two depends on the Code:

# Define debug

# UNDEF debug

In this case, debug is not defined. If the two are in another order, the compiler considers debug to be defined.

  2. # If, # Elif, # else, # endif

This tells the compiler to control the process of compiling code. Consider the following code:

  

# If debug

Console. Write ("debug ");

# Elif release

Console. Write ("realse ");

# Else

Console. Write ("other ");

# Endif

The above code means that if debug is defined, debug is output, and release is defined, realse is output; otherwise, other is output. What if debug and realse are defined? You can give it a try.

 

  3. # warning, # error

The two parameters can tell the compiler whether a warning or error message is returned. Except for the error message, compilation stops.

Related message: Pre-compiled commands for C # essential knowledge Author: Alen Source: IT expert network forum

Pre-compilation is generally used to prevent repeated inclusion and compilation of header files. The pre-compilation command indicates the operations performed by the compiler before the program is officially compiled, and can be placed anywhere in the program. This article briefly introduces the pre-compiled commands in C.

 

Refer to the following code:

  

# If debug ------> if the current debug status is selected as debug, the default value is # define.

# Warning "the current status is ddbug"

# Elif release ------> If the release status is selected, there is no default value for this. I do not know if my test is correct.

# Warning "the current status is realse"

# Else

# Error "and identify the status"

# Endif

  4. # region and # endregion

 

These two are used to form a code block.

  5. # Line (the specified use is not clear, refer to msdn)

This command can change the file name and line number information displayed in the warning and error messages of the Jian compiler, and use # line default to restore the line number to the default line number.

The following example shows how to report two warnings associated with the row number. # Line 200 Command forces the row number to be 200 (although the default value is #7 ). The result of another line (#9) as the default # Line command follows the normal sequence.

Example 1:

  

// Preprocessor_line.cs

Public class myclass2

{

Public static void main ()

{

# Line 200

Inti; // cs0168 on line 200

# Line default

Char C; // cs0168 on line 9

}

}

Example 2:

 

The following example shows how the debugger ignores hidden rows in the code. When running this example, it displays three lines of text. However, when you set the breakpoint as shown in the example and press the F10 key to pass the code one by one, you will see that the debugger ignores hidden lines. Note that the debugger ignores the breakpoint even if it is set on the hidden line.

  

// Preprocessor_linehidden.cs

Using system;

Class myclass

{

Public static void main ()

{

Console. writeline ("normal line #1."); // set a break point here.

# Line hidden

Console. writeline ("hidden line .");

# Line default

Console. writeline ("normal line #2 .");

}

}

Related message:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.