C进阶养成记 - 养成记21:宏定义与使用分析

--事物的难度远远低于对事物的恐惧!

    这个章节我们来谈一谈C语言中的宏定义与使用分析,相信每个学过C的都知道,在C中,宏具有如下三个特性:

    -#define是预处理器处理的单元实体之一

    -#define定义的宏可以出现在 程序的任意位置

    -#define定义之后的代码都可以使用这个宏

    而在C语言中,我们常常用#define来定义宏常量,#define定义的宏常量可直接,其本质为字面量,所以宏常量是不占用内存空间的(const定义的只读变量是占用内存空间的)。下边来看看几个宏常量的定义,正确吗?

#define ERROR -1
#define PATH1 "D:\test\test.c"
#define PATH2 D:\test\test.c
#define PATH3 D:\test\
test.c

int main()
{
    int err = ERROR;
    char* p1 = PATH1;
    char* p2 = PATH2;
    char* p3 = PATH3;
}

首先我们用预处理器来预处理一下,预处理通过(因为预处理是不进行语法检查的),并且得到的预处理文件内容如下,从预处理文件得知,main函数里的宏都被具定义的内容所替换。

#line 1 "21-1.c"


int main()
{
    int err = -1;
    char* p1 = "D:\test\test.c";
    char* p2 = D:\test\test.c;
    char* p3 = D:\testtest.c;
}

所以说,上边的四个宏定义是正确的,能通过预处理器,但是PATH2余PATH3不能通过编译,因为语法不符(大家可自行实验)


说完了宏常量,我们来说说宏表达式 

    -#define表达式的使用类似于函数调用

    -#define表达式可以比函数更强大

    -#define表达式比函数更容易出错

下边来看看几个宏表达式,执行结果是否正确:

 #include <stdio.h>

#define _SUM_(a, b) (a) + (b)
#define _MIN_(a, b) ((a) < (b) ? (a) : (b))
#define _DIM_(a) sizeof(a)/sizeof(*a)

int main()
{
    int a = 1;
    int b = 2;
    int c[4] = {0};

    int s1 = _SUM_(a, b);    
    int s2 = _SUM_(a, b) * _SUM_(a, b);
    int m = _MIN_(a++, b);
    int d = _DIM_(c);    

     printf("s1 = %d\n", s1);
     printf("s2 = %d\n", s2);
     printf("m = %d\n", m);
     printf("d = %d\n", d);

    return 0;
}

好了,我们来编译运行一下


从运行结果,我们知道s2、m的输出结果与我们所预期的不一样,为什么会是这样?别急,我们先来看看预处理后的文件,如下:

#line 1 "21-2.c"

int main()
{
    int a = 1;
    int b = 2;
    int c[4] = {0};

    int s1 = (a) + (b);
    int s2 = (a) + (b) * (a) + (b);    //原来这样。。
    int m = ((a++) < (b) ? (a++) : (b));    //原来这样。。
    int d = sizeof(c)/sizeof(*c);

    return 0;
}

经过预处理宏展开后,我们看到s2与m变量的值,现在相信你已经能理解了,这也是宏表达式比函数更容易出错。

宏表达式与函数的对比

    -宏表达式被预处理器处理,编译器不知道宏表达式的存在

    -宏表达式用 "实参" 完全替代形参,不进行任何运算

    -宏表达式没有任何"调用"开销

    -宏表达式不能出现递归定义

思考:宏表达式是否存在作用域限制问题?下边的代码是否能正常编译?

#include <stdio.h>

void def()
{
#define PI 3.1415926
#define AREA(r) r * r * PI
}

double area(double r)
{
    return AREA(r);
}

int main()
{
    double r = area(5);

    printf("pi = %f\n", PI);
    printf("d = 5; a = %f\n", r);

    return 0;
}

我们来编译运行一下:


很明显,能编译运行,我们在单独用预处理器处理下(这里把头文件与打印语句注释掉,因为预处理后会展开很多东西),看看中间文件,

#line 1 "21-3.c"


void def()
{
    
    
}

double area(double r)
{
    return r * r * 3.1415926;
}

int main()
{
    double r = area(5);

    return 0;
}

从中间文件,可以看出,编译器根本不知道#define宏常量的存在,所以#define宏也就没有什么作用域限制。这也符合文章开头我们所说的:#define定义之后的代码都可以使用这个宏


来看看C语言中几个强大的内置宏


下半边以一个综合示例来展示下#define宏的魅力:

#include <stdio.h>
#include <malloc.h>

#define MALLOC(type, x) (type*)malloc(sizeof(type)*x)

#define FREE(p) (free(p), p=NULL)

#define LOG(s) printf("[%s] {%s:%d} %s \n", __DATE__, __FILE__, __LINE__, s)

#define FOREACH(i, m) for(i=0; i<m; i++)
#define BEGIN {
#define END   }

int main()
{
    int x = 0;
    int* p = MALLOC(int, 5);

    LOG("Begin to run main code...");

    FOREACH(x, 5)
    BEGIN
    	p[x] = x;
    END

    FOREACH(x, 5)
    BEGIN
    	printf("%d\n", p[x]);
    END

    FREE(p);

    LOG("End");

    return 0;
}

运行结果:



总结:

    1、预处理器直接对宏进行文本替换

    2、宏使用时的参数不会进行求值和运算

    3、预处理器不会对宏定义进行语法检查

    4、宏定义时出现的语法错误只能被编译器检测

    5、宏定义的效率高于函数调用

    6、宏的使用会带来一定的副作用

猜你喜欢

转载自blog.csdn.net/lms1008611/article/details/80036250