C must pay attention to the details of the macro definition

1, can not ignore the macro definition of space, as one example:

#define f (x) ((x)-1)

When the pre-compiled, due to macro definition is merely an alternative effect, so, if f (x) when replaced as follows:

(x) ((x)-1)(x)

Thus, far short of the requirements.


2, and rename the macro definition, if the following: `

#define C1 struct foo*
typedef struct foo* C2
C1 a, b;
C2 a, b;

Analysis: The two are completely different.

(1) C1 a, b are substantially equivalent to the struct foo * a, b; this is a pointer to a structure, and b is a structure.

(2) C2 a, b; is substantially equivalent to the struct foo * a, b; if both are pointers to structures.


3, with the macro parameters Note: Because the program calls the function when executed, would generate call overhead, a lot of the time, if it is relatively short feature with a parameterized macro to achieve, because the macro definition is expanded in situ pre-compile time, so there will be no call overhead, such as:

#include <stdio.h>
#define max(x,y) (x)>=(y)? (x):(y)
int Max(int x, int y)
{
    int max;
    max = (x)>=(y)? (x):(y);
    return max;
}
    int main()
{
    int num1 = 0;
    int num2 = 0;
    num1 = Max(2,5);
    num2 = max(2,5);
    printf("The max is %d(func), %d(define)\n", num1, num2);
    return 0;
}

Operating results as follows:

The max is 5(func), 5(define)

Two implementations can be seen that the function is the same, however, when the parameters x, y side effects (effects parameters is itself functions such as: i ++, i- arithmetic function with such parameters), to see the following one example:

#include <stdio.h>
#define max(x,y) (x)>=(y)? (x):(y)
int Max(int x, int y)
{
    int max;
    max = (x)>=(y)? (x):(y);
    return max;
}
    int main()
{
    int num1 = 0;
    int num2 = 0;
    int i = 2;
    int j = 2;
    num1 = Max(2,++i);
    num2 = max(2,++j);
    printf("The max is %d(func), %d(define)\n", num1, num2);
    return 0;
}

Examples of parameters that have just just replaced (2, ++ i), (2, ++ j), the other has not changed, its operation result:

The max is 3(func), 4(define)

However, operating results are not the same. Analysis:
(1) with respect to the parameters of the function, parameter passing processes are essentially first parameter passed to the first copy of the stack as a function of local variables inside, so that a copy number instead of a process (such as: ++ i passed, the essence is the result ++ i 3 y copy then pass, after which y is 3), so the function max in the comparison is of size 2 and 3, the result is 3.

(2) with respect to a reference macro definition, since the functions defined by the macro is expanded in situ, so that the macro expands to a substantially defined above of:

(2)>=(++i)? (2):(++i);

So when executed? The previous time, will go through a ++ i calculation, so the result ++ i is 3, and (2)> = (++ i)? The result is, so the back of the expression is executed, and therefore executed once again ( ++ i), and therefore the results of 4.

发布了24 篇原创文章 · 获赞 27 · 访问量 1万+

Guess you like

Origin blog.csdn.net/gyyu32g/article/details/86504096