注:博客中内容主要来自《狄泰软件学院》,博客仅当私人笔记使用。
测试环境:Ubuntu 10.10
GCC版本:4.4.5
一、C语言中的宏定义
1)#define是预处理器处理的单元实体之一
2)#define定义的宏可以出现在程序的任意位置
3)#define定义之后的代码都可以使用这个宏
二、定义宏常量
1)#define定义的宏常量可以直接使用
2)#define定义的宏常量本质为字面量
下面的宏常量定义正确吗?
#define ERROR -1
#define PATH1 "D:\test\test.c"
#define PATH2 D:\test\test.c
#define PATH3 D:\test\ ——这个反斜杠是接续符,并不是路径
test.c
实例分析
宏定义分析
21-1.c
#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;
return 0;
}
操作:
1) 预编译:gcc -E 21-1.c -o 21-1.i
2) 查看21-1.i文件:
# 1 "21-1.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 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; //最后一个‘\’被认为是接续符,不是路径
return 0;
}
三、宏定义表达式
1)#define表达式的使用类似函数调用
2)#define表达式可以比函数更强大
3)#define表达式比函数更容易出错
下面的红表达式定义正确吗?
#define _SUM_(a,b) (a)+(b)
#define _MIN_(a,b) ((a) < (b):(b))
#define _DIM_(a) sizeof(a)/sizeof(*a) //数组大小
实例分析
宏表达式分析
21-2.c
// #include <stdio.h>
#define _SUM_(a, b) (a) + (b)
#define _MIN_(a, b) ((a) < (b) ? (a) : (b))
#define _DIM_(a) sizeof(a)/sizeof(*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;
}
操作:
1) 预编译:gcc -E 21-2.c -o 21-2.i
2) 打开预编译文件:
# 1 "21-2.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 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); //本意:(a+b)*(a+b)
int m = ((a++) < (b) ? (a++) : (b)); //本意:((a++) < (b) ? (a) : (b))
int d = size(c)/sizeof(*c);
return 0;
}
提示:使用宏函数时,可以先用预编译指令,查看编译后的文件内容,检查错误。
四、宏表达式与函数的对比
1)宏表达式被预处理器处理,编译器不知道宏表达式的存在
2)宏表达式用"实参"完全替代形参,不进行任何运算
3)宏表达式没有任何的"调用"开销
4)宏表达式中不能出现递归定义
#define _SUM(n) ((n>0)?(_SUM_(n-1)+n):0)
int s = SUM_(10);
五、有趣的问题……
1)宏定义的常量或表达式是否有作用域限制?
下面的程序合法吗?
void def()
{
#define PI 3.1415926
#define AREA(r) r*r*PI
}
double area(double r)
{
return AREA(r);
}
实例分析
宏的作用域分析
21-3.c
// #include <stdio.h>
int a = AREA(5);
void def()
{
#define PI 3.1415926
#define AREA(r) r * r * PI
}
double area(double r)
{
int a = AREA(5);
return AREA(r);
}
int main()
{
double r = area(5);
//printf("PI = %f\n", PI);
//printf("d = 5; a = %f\n", r);
return 0;
}
操作:
1) 预编译:gcc -E 21-3.c -o 21-3.i
2) 打开文件:
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "21-3.c"
int a = AREA(5); //宏没有生效
void def()
{
//宏定义之前的位置
}
double area(double r)
{
int a = 5 * 5 * 3.1415926;
return r * r * 3.1415926;
}
int main()
{
double r = area(5);
return 0;
}
分析:
宏定义后边的内容可以使用该宏定义之后的文本。
六、强大的内置宏
实例分析
宏使用综合示例
21-4.c
#include <stdio.h>
#include <malloc.h>
//申请内存
#define MALLOC(type, x) (type*)malloc(sizeof(type)*x)
//释放内存并指针为NULL
#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) gcc 21-4.c -o 21-4.out编译正确,打印结果:
[Jan 29 2018] {21-4.c:19} Begin to run main code ...
0
1
2
3
4
[Jan 29 2018] {21-4.c:33} End
小结
1)预处理器直接对宏进行文本替换
2)宏使用时的参数不会进行求值和运算
3)处理器不会对宏定义进行语法检查
4)宏定义时出现的语法错误只能被编译器检测
5)宏定义的效率高于函数调用
6)宏的使用会带来一定的副作用