A brief analysis of the macro definition of C language __c language

Source: Internet
Author: User
motives

A few days ago to see an interview question: Write a "standard" macro min, this macro enters two parameters and returns the smaller one, which mentions two topic points: knowing how to carefully enclose the arguments in parentheses in a macro is a side effect of a macro, such as what happens when you write the following code. least = MIN (*p++, b);

Usually will also use the macro definition, the understanding of the macro is not particularly in-depth, look at the Kernighan of the C programming language about the Macro section, the special record. the nature of a macro definition

Replace text #define Name

The essence of a macro is code substitution, which is a very long piece of code that uses a simple name to replace the short name in the compilation process. advantages and disadvantages of macro definition

Advantages: Improve the readability of the program, at the same time, it is convenient to modify and improve the running efficiency of the program: the function of function call can be accomplished by using the macro definition with parameters, and the function can be avoided, the stack and the stack operation are reduced, the system overhead is improved, and the running efficiency is increased A string operation can accomplish many functions that the compiler cannot implement. Like # #连接符.

Note: The # # Join symbol is made up of two well numbers, and its function is to join two substrings (token) in a macro definition with parameters to form a new substring. But it cannot be the first or last substring. The so-called substring (token) refers to the smallest syntactic unit that the compiler can recognize. Plus # #就是为了把宏定义中的参数识别出来

Disadvantage: For the macro with the parameter, because it is a direct replacement, and will not check whether the parameters are legitimate, there are security risks. Because precompiled statements are simply value substitution, the lack of a type of detection mechanism. Such a preprocessing statement cannot enjoy the benefits of strict type checking in C + +, which may be a potential cause of a series of errors. Too many nested definitions can affect the readability of the program and are prone to errors; because it is embedded directly, the code may be a little bit more. use macro definition Considerations 1. Carefully enclose the parameters in parentheses in the macro

Why would you do that? The important function of parentheses is to specify the order of calculation. Just observe the macro definition you know the order of the calculations, but put the code fragment into the entire code, and if there are no parentheses, the original order of calculation will be disrupted. For example:

#define Square (x) x * x

Defines a macro definition that asks for a number of peace, but. If you call the macro definition with square (x+1), the compile-time code is: x+1 * x + 1 is not at all the expected order of calculation, after full substitution of the full code. The macro definition above should read:

#define Square (x) ((x) * (x))

When you call the macro definition with square (x+1), there is no problem at all. The outermost bracket is to isolate the macro definition of the entire operation from the external code. 2. Avoid the side effects of macros

The definition of a macro has a side effect, and if there is an increase in self subtraction or input/output in an expression, an error occurs. If you use square (x + +) to invoke the above macro definition, then the resulting code (x + +) is what you expect.

[1]. Kernighan B W, Ritchie D, Xu Baowen, et. C programming language [M]. Mechanical Industry Publishing House, 2004.
[2]. C++/c the meaning of # # connectors and # characters in a macro definition
[3]. The pros and cons of macro definitions

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.