【C】21.宏定义与使用分析

#define介绍

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

定义的宏可以出现在程序的任意位置,定义之后代码都可以使用这个宏。

预处理器不会进行语法分析的(直接进行文本替换),后面的编译器才进行语法分析,这个过程很容易产生歧义错误。

#define 定义的宏常量可以直接使用

#define 定义的宏常量本质为字面量

#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;
}

预处理处理结果(没有任何编译错误)

编译结果,可以发现是有编译错误的。

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

#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中已经改变了程序本来的意思!!

#define _SUM_(n) ((n>0)? (_SUM_(n-1)+n):0)

宏定义的常量或者表达式没有作用域限制。

宏定义的常量或表达式是没有“作用域的”(即上面定义下面就可以使用了,因为宏定义是在预编译处理阶段就被替换了,而编译器不知道这些替换的存在),C语言中作用域是针对变量和函数的。

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;
}

内置宏

#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.宏定义效率高于函数调用,当然也会带来一定的副作用。

发布了84 篇原创文章 · 获赞 0 · 访问量 767

猜你喜欢

转载自blog.csdn.net/zhabin0607/article/details/103247814
21.