展开宏以方便阅读

展开宏以方便阅读

展开宏以方便阅读 #

gcc -E macro.c -o macro_expand.c.

-E Preprocess only; do not compile, assemble or link.

macro.c:

#include <stdio.h>
#include <stdlib.h>

#define SUM(a, b) ((a) + (b))
#define SUB(a, b) ((a) - (b))

int main()
{
    int a = 2;
    int b = 1;
    int c = 0;

    c = SUM(a, b) + SUB(a, b);
    printf("%d\n", c);
}

生成的macro_expand.c:

// 生成的文件里包含了非常多内容,这里就不一一展示了,有兴趣的可以自己试一下

// 其中关键的部分:
# 7 "macro.c"
int main()
{
    int a = 2;
    int b = 1;
    int c = 0;

    c = ((a) + (b)) + ((a) - (b));
    printf("%d\n", c);
}