DEFINE introduction and multi-line macro definition in C language, define multi-line

Source: Internet
Author: User

[Switch] DEFINE introduction and multi-row macro definition in C language, define multiple lines

[Turn] Introduction to DEFINE in C and Multi-line Macro Definition

To write a good C language, beautiful macro definitions are very important. Macro definitions can help us prevent errors, improve the portability and readability of our code, and more.

In the software development process, there are often some commonly used or general functions or code segments. These functions can be written as functions or encapsulated into macro definitions. So is it better to use functions or macros? This requires us to make a reasonable choice between the two.

Let's look at an example, comparing two numbers or expression sizes. First we write it as a macro definition:

#Define MAX (a, b) ((a)> (b) (a): (b))

Second, implement it with functions:

Int max (int a, int b)

{

Return (a> b a: b)

}

Obviously, we do not choose to use functions to accomplish this task. There are two reasons: First, the function call will bring additional overhead. It needs to open up a stack space, record the return address, push the formal parameters on the stack, and return from the function. Also release the stack. This overhead will not only reduce the code efficiency, but also increase the amount of code. The use of macro definitions is better than functions in terms of code size and speed. Second, the parameters of the function must be declared as a specific type. So it can only be used on the appropriate type of expression. If we want to compare the size of two floating-point types, we have to write a comparison function specifically for floating-point types. Conversely, the above macro definition can be used for integers, long integers, single floating point, double floating point, and any other type that can be compared with the size of the value using the ">" operator, that is, the macro is independent of the type .

Compared to using functions, the disadvantage of using macros is that each time a macro is used, a copy of the macro definition code is inserted into the program. Unless the macro is very short, using a macro can significantly increase the length of the program.

There are some tasks that cannot be implemented with functions at all, but are well implemented with macro definitions. For example, parameter types cannot be passed to functions as parameters, but parameter types can be passed to macros with parameters.

Look at the following example:

#Define MALLOC (n, type) \

((Type *) malloc ((n) * sizeof (type)))

Using this macro, we can allocate a certain size of space for any type and return a pointer to this space. We can observe the exact working process of this macro:

Int * ptr;

Ptr = MALLOC (5, int);

的 The result of expanding this macro:

Ptr = (int *) malloc ((5) * sizeof (int));

This example is one of the classic applications of macro definitions. It completes functions that cannot be completed by functions, but macro definitions cannot be abused. Generally, if the same code needs to appear in several places in the program, a better way is to implement it A function.

The following summarizes the differences between macros and functions for everyone to use when writing code. This summary is taken from the book "C and pointers".

[Turn] Introduction to DEFINE in C and Multi-line Macro Definition

EXample

single line definition of define

#define maxi (a, b) (a>; b? a: b)
Multi-line definition of define

define can replace multiple lines of code, such as macro definitions in MFC (very classic, although disgusting)

#define MACRO (arg1, arg2) do {\
   \
stmt1; \
stmt2; \
   \
} while (0)
The key is to add a "\" to every line break


// The macro definition writes swap (x, y) exchange function
#define swap (x, y) \
x = x + y; \
y = x-y; \
x = x-y; 


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.