preprocessing directives in C #

Source: Internet
Author: User

Unlike C + +, C # does not have a stand-alone preprocessor. The preprocessing directives in C # (pre-processing directives) are only used to align with C, rather than a separate processing step before the compiler starts compiling the code, which is executed as part of lexical parsing.

preprocessing directives begin with the # sign and at the beginning of the line (spaces can appear before). Before introducing conditional compilation, we first learn two preprocessing directives for defining symbols and canceling symbol definitions: #define和 #undef.

#define指令对于有一点C语言知识的读者来说再熟悉不过, it is very similar to the macro definition in C:

#define COUNT

The symbol count is defined here, and its scope is the entire file where the definition resides. It is important to note that the symbol definition must precede all other statements, or be placed in front of all real code. So the following code is wrong:

Using System
#define COUNT

Take another look at the example:

#define A

Namespace N
{
#define B
#if B
Class class1{}
#endif
}

It is wrong to appear "real code" before the second #define instruction.

However, the following code is legal because #if is not "real":

#define A

#if A
#define B
#endif

Namespace N
{
#if B
Class class1{}
#endif
}
Sometimes we want to cancel the definition of a symbol in the source program, and then we need to use the #undef directive:

#undef COUNT

As with #define, #undef的作用域也是定义所在的整个文件, it must also appear before all "real code". The following example defines a symbol and then cancels it, and the second #undef directive, although it has no effect, is legal:

#define A

#undef A

#undef A

Well, after simply introducing the preprocessing statements, we can learn how to use the defined symbols for conditional compilation.

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.