Pre-processing program

Source: Internet
Author: User

--------Preprocessor--------

    • The preprocessor statement uses the pound sign (#) notation, which must be the first non-whitespace character in a row.

  • #define STATEMENTS (which are often referred to as defining "Macros"):
    • One of the basic uses of #define statements is to assign program constants to symbolic names. For example:
    • #define  Constant  1
      do not add a semicolon after the preprocessor statement (;), because the pre-defined name appears, the preprocessor replaces all characters on the right to the program. Therefore, you do not need to add a semicolon (;) after the preprocessor statement, or it will be replaced with the program.



    • The code defines the program constant Constant and makes his value equal to 1. After that, Constant can be substituted for any place that requires a constant of 1. The preprocessor replaces this name (Constant) with a predefined value of 1 in the program.
    • The preprocessor replaces all characters nonalphanumeric the right of the predefined names in the #define statement with the program. So on the right side of the predefined name, it can be a constant, a statement, a code, or anything that makes sense.
    • #define statements are often placed at the beginning of a program, but after #import or include statements. This is not necessary and they can appear anywhere in the program. However, you must define them before applying the name to the program.
    • Predefined names and variables behave differently, with no local definitions or the like. After you define a name, you can use it anywhere in your program.
    • The predefined value itself can refer to another predefined value.
    • to define a macro with parameters:
      • when you define a name with parameters, spaces are not allowed between the opening parentheses of the parameter list for a predefined name.

        The general form of a macro definition with parameters is as follows: #define < macro name > (< parameter table >) < macro body >

        where < macro name > is an identifier,< parameter table > parameter can be one or more, depending on the case, when there are multiple parameters, each parameter is separated by commas. < macro body > is the replaced string, and the string in the macro body is an expression that consists of the parameters in the parameter table. For example:

        #define SUB (A, A, b) a

        If the following statement appears in the program:

        Result=sub (2, 3);

        is replaced by the following:

        result=2-3;

        If the following statement appears in the program:

        result= SUB (x+1, y+2);

        is replaced by the following:

        result=x+1-y+2;

        In such a macro substitution process, in fact, only the parameters in the parameter table into the expression of the macro body, in the example above, that is, the expression of A and B in the 2 and 3 respectively into.

        We can see that the macro definition with a parameter is similar to a function. If we treat the arguments that appear when the macro is defined as formal parameters, the parameters that appear when the macro definition is referenced in the program are treated as arguments. Then A and B in the example above are formal parameters, while 2 and 3 and x+1 and y+2 are arguments. When you replace a macro, it replaces the formal parameter in the < macro body > with an argument.

    • Preprocessor line break rules:
      • Typically, a preprocessor hypothesis definition is included in a line of the program. If a second row is required, the last character of the previous line must be a backslash line symbol (\). This character tells the preprocessor that there is a follow-up here. This is also true for multiple subsequent rows.

  • #import Statement (import file Directive):
    • One of the best abilities to import files is that he is you are able to centralize the definitions so that all programs apply the same values.
    •  //  example:    

       

    • In objective-c , #import is used as an improved version of #include instructions. In addition, #import determine that a file can only be imported once, which allows you to have no problem with recursive inclusion
      @class is commonly used in header files to declare an instance variable of the class, and in M files it is necessary to use #import
      @class is to tell the compiler that there is such a class, as to what the definition of the class is.
      @class is commonly used in header files to declare an instance variable of the class, and in M files it is necessary to use #import
      What's the difference between #import<> and #import?
      "" means to create your own files, with this first will be in the current directory to look for, if not found on the other location to find, "" and <> is the relative one is the file you created is a system file

  • Conditional Compilation:
    • #ifdef, #endif, #else和 #ifndef statement:  
       //   #ifdef   Ipad#define kimagefile  @ " barnhd.png    #else   @ " barn.png  Span style= "color: #800000;" > "  #endif 

      //if the program compiles, #ifdef The symbol specified in the row has been defined by a #define statement or command line, the compiler will process the program segment starting from here to the #else, #elif, or #endif, otherwise the segment will be ignored.

       

    • #if and #elif Preprocessor statements:
      • An If statement can be used to detect whether a constant expression is not 0. If the expression result is nonzero, all subsequent rows to the #else, #elif, or #endif position are processed. Otherwise they will be skipped.
    • // the value of the mac_os_x_version_min_required is compared to the value of the mac_os_x_version_10_5 of the predefined variable. If the former is less than the latter, the subsequent #define is processed

      #if Mac_os_x_version_min_required < Mac_os_x_version_10_5

      #define Nsmaximumstringlength (int_max-1)
      #endif


      //Special operators
      define (name) can also be used in the # if statement

      Defined (DEBUG)
      ......
      #endif

      And

      #if DEBUG
      ......


      Have the same effect

    • #undef statement:
      • In some cases, it may be necessary to make some defined names undefined, which can be achieved by using #undef statements.
      • To eliminate the definition of a specific name, write the following statement
      • #undef Name

Preprocessor

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.