C language-preprocessing and C language preprocessing

Source: Internet
Author: User

C language-preprocessing and C language preprocessing

References: C Primer Plus

1. Character ing

In the first step of C language preprocessing, the pre-processor will map the characters in the source code. If there are no special requirements, this step will not change the characters in the source code. If the programmer needs it, the character ing can convert extended three-element characters into source characters, such ?? = Convert #,?? /. This is because not all keyboards can use the # symbol. Therefore, the rules are mapped to the rule.

2. '\' + '\ N'

When the Preprocessor continuously reads the backslash and linefeed, the two characters are deleted, so that the current physical row and the next physical row are merged into a logical row. This method can be used to define complex macros or strings for easy line feed reading. For example:

  printf("That's wond\erful!\n");

Equivalent

  printf("That's wonderful!\n");

3. Notes

The Preprocessor replaces the Annotation with a space character.

4. Preprocessing commands

The Preprocessor uses all strings starting with # and ending with line breaks as a preprocessing instruction, but this rule is not affected by the combination of '\' + '\ n, therefore, preprocessing commands can be expressed in multiple lines.

  4.1 # define-basic

# The define command can replace strings. There are three parts: # define, Macro, and replacement list. Macros can be classified into Class Object macros or class function macros. macros are named in the same way as variable identifiers. They can contain letters, numbers, and underscores, but cannot start with numbers. If parentheses appear in the macro, they are processed as class function macros. Otherwise, they are considered Class Object macros. For example

#define PX printf("X is %d.\n",x)#define PR(Z) printf("The result is %d.\n", Z)int main() {    int x=0;    PX;    PR(x);        return 0;        }

The result after preprocessing is

int main() {    int x=0;    printf("x is %d",x);    printf("The result is %d.\n",x);    return 0;}

# Define supports redefinition. After the Code defines a MACRO using # define, you can use # define later to change the definition of MACRO.

  4.2 # define-###, #,..., _ VA_ARGS __

    # You can bond identifiers. For example, if # define INT (name) x # name is defined, INT (1) is converted to x1 because # define text is replaced by an identifier, if you define # define INT (name) xname directly, because xname and name are not the same identifier, INT (1) cannot be converted to x1.

# The identifier can be stringized. This function can be used with the character string connection feature to solve the text Replacement Problem of the string, because # define does not replace the text of the string content. For example, # define PSQR (X) printf ("The square of X is % d. \ n ", (X) * (X); the first X in the replacement list will not be replaced. The solution is to write it as # define PSQRprintf ("The square of # X" is % d. \ n ", (X) * (X )));

... And _ VA_ARGS _ can implement variable parameter macros, such as definition # define PR (...) printf (_ VA_ARGS _), use PR to replace the printf function. Note that... can only be used as the last parameter. # define FPR (X,..., Y) fprintf (X ,__ VA_ARGS __, Y) is incorrect.

 

 

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.