C Primer Plus reading notes-preprocessor directives

Source: Internet
Author: User

#define is often seen in the code, #ifdef

#define: The apparent constant general instruction length is limited to one line of code, except for the \ (backslash line) can be extended to the next line.

In general, each # define row consists of three parts.

    1. The first part is the # define directive itself
    2. The second part is the selected abbreviation, which is called macro. There are no spaces allowed in the name of the macro! Use only letters, underscores (_), and numbers, and the first character cannot be a number
    3. The third part (#define行除了第一第二部分外的部分). Called a replacement list or principal.
    •  after the preprocessor finds an instance of the macro in the program, it always replaces the macro with a body, with one exception .

macros can also use parameters to create class function macros that are similar in shape and function to functions. The parameters of the macro are enclosed in parentheses.

  

  The parameters of the macro are not exactly the same as the parameters of the function

When the program runs, the function call passes the value of the parameter to the function, and before compiling, the macro calls the language symbol of the argument to the program.

#define Square (x) x*xx=4evaluating Square (x): The result is  a      //aevaluating square (2): The result is  4 B evaluating SQUARE (x+2): The result is a  //c evaluating 100/square (2): The result is a  //d

For A, B results are the same. But the other results are different.

The original x*x parameter in C should be x+2*x+2

D is 100/2*2, so we should count 100/2 first.

Conditional compilation:

#ifdef MAVIS

#define X 5

#else

#define X 6

#endif

#ifdef指令说明, if the preprocessor has defined the following marker MAVIS, execute all instructions and compile the code until the next #else or #endif appears. If no subsequent glyph is defined Mavis executes the instructions followed by #else and compiles until #endif. Similar to the if else in C. The #ifndef, in other words, determines whether the subsequent glyph is undefined, and executes the following if the subsequent marker is undefined.

#if

#elif more like the IF in C,

These are generally followed by constant integer expressions, except that 0 is true. Defined is a preprocessing operator//defined (para), returns true if his arguments have been defined with # define, otherwise returns 0

C Primer Plus reading notes-preprocessor directives

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.