To write a good C language, a beautiful macro definition is very important. Macro definitions can help us prevent errors, improve the portability and readability of code, and so on.
During software development, there are often common or common functions or snippets that can be written as functions or encapsulated as macro definitions. So whether to use the function is good, or macro definition good. This requires us to make a reasonable choice between the two.
A macro definition that allows you to include more than two lines of command. At this point must be at the rightmost plus "\" and the line "\" After no more characters, even the annotation section can not have, the following each line of the last must be "\", "\" plus a space will be an error, but not with the annotation.
Let's take a look at an example, compare two numbers or the size of an expression, first we write it as a macro definition:
#define MAX (A, B) ((a) > (b) (a): (b))
Second, it is implemented using functions:
int max (int a, int b)
{
Return (a > B a:b)
}
Obviously, we don't choose to use functions to do this task for two reasons: first, the function call brings extra overhead, it needs to open up a stack of space, record the return address, the form parameter stack, return from the function and release the stack. This overhead will not only reduce code efficiency, but also greatly increase the amount of code using a macro definition is better than a function in terms of code size and speed; second, the parameter of the function must be declared as a specific type, so it can only be used on a type-appropriate expression, and if we want to compare the size of two floating-point types, You'll have to write a comparison function specifically for floating-point types. Conversely, the above macro definition can be used for shaping, long shaping, single floating-point type, double floating-point type, and any other type that can compare the value size with the ">" operator, that is, the macro is type Independent.
The disadvantage of using a macro is that a copy of the macro definition code is inserted into the program each time you use the macro, as opposed to using a function. Unless the macro is very short, using a macro increases the length of the program significantly.
There are some tasks that cannot be implemented using functions at all, but they are well implemented with macro definitions. For example, a parameter type cannot be passed as a parameter to a function, but the parameter type can be passed to the macro with the argument.
Look at the following example:
#define MALLOC (n, type) \
((Type *) malloc ((n) * sizeof (type))
With this macro, we can assign any type of space we specify, and return a pointer to that space. We can look at the exact working process of this macro:
int *ptr;
ptr = MALLOC (5, int);
Expand this macro to the following results:
PTR = (int *) malloc ((5) * sizeof (int));
This example is one of the classic applications of the macro definition, which completes functions that cannot be completed, but the macro definition cannot be abused, and it is usually better to implement it as a function if the same code needs to appear in several parts of the program.
Here's a summary of the differences between macros and functions to use when you write code, which is excerpted from the C and Pointers book.
EXample
Define single-line definition #define MAXI (a,b) (A>;B?A:B)
Multiple-line definition of define
Define can replace more than one line of code, such as the macro definition in MFC (very classic, although it makes people look disgusting)
#define MACRO (arg1, arg2) do {\
\
STMT1; \
STMT2; \
\
} while (0)
The key is to add a "\" to each line change.
Macro definition writes out the swap function of swap (X,Y)
#define SWAP (x, y) \
x = x + y;\
y = x-y;\
x = XY;
A small example of your own writing:
Cannot add * should be a pointer is also a type
#define SORT (A, n) \
{\
int I, j;\
int *t = MALLOC (1,int); \
For (i=0 i<n-1; i++) \
{\
For (j=0 j<n-1-i; j + +) \
{\
if (* (A+J) > * (a+j+1)) \
{\
*t = * (a+j); \
* (A+J) = * (a+j+1);
* (a+j+1) = *t;\
}\
}\
}\
}
int main (int argc, const char * argv[])
{
int a=10, b= 120;
int data[]={3,200,5};
Swap (&a, &b);
Sort (data, 3); Like queues: Automatically identify types based on topic (Shi can) parameters
SORT (data, 3); Takes the active argument instead of the formal parameter recognition type
for (int i=0;i<3;i++)
cout << Data[i] << Endl;
printf ("%d%d", A, b);
return 0;
}