#define是预处理器处理的单元实体之一;
#define定义的宏可以出现在程序的任意位置;
宏表达式被预处理器处理,编译器不知道宏表达式的存在;
宏表达式用“实参”完全替代形参,不进行任何运算;
宏表达式没有任何的“调用”开销;
为什么呢?
预处理器直接对宏进行文本替换;
宏使用时的参数不会进行求值和运算;
预处理器不会对宏定义进行语法检查;
宏定义时出现的语法错误只能被编译器检测;
宏定义的效率高于函数调用;
宏的使用会带来一定的副作用。
#define定义的宏可以出现在程序的任意位置;
#define定义之后的代码都可以使用这个宏。
#define定义的宏常量可以直接使用;
#define定义的宏常量本质为字面量(不占用内存<只读存储区>—和const常量(变量,占内存)的本质区别)。
下面的宏定义正确吗?
#define error -1 //
#define PATH1 "D:\test.c"
#define PATH2 D:\test.c
#define PATH3 D:\test\ //
test.c
编译运行:
~/will$ gcc -E test.c -o test.i //预编译没报错
int main()
{
int err = -1;
char* p1 = "D:\test.c";
char* p2 = D:\test.c; //error
char* p3 = D:\testtest.c; //error
}
~/will$
~/will$ gcc test.c
test.c: In function ‘main’:
test.c:13: error: ‘D’ undeclared (first use in this function)
test.c:13: error: (Each undeclared identifier is reported only once
test.c:13: error: for each function it appears in.)
test.c:13: error: expected ‘,’ or ‘;’ before ‘:’ token
test.c:13: error: stray ‘\’ in program
test.c:14: error: expected ‘,’ or ‘;’ before ‘:’ token
test.c:14: error: stray ‘\’ in program
预编译过程不进行语法语义判断,表明宏定义是对的,但是编译后报错,不合C语言的语法规范。
const常量本质是变量,占内存;
宏常量不是变量,不占内存。
#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 = 3;
int b = 4;
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("a = %d\n", a);
printf("m = %d\n", m);
printf("d = %d\n", d);
return 0;
}
先单步编译:gcc -E test.c -o test.i
得到: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);
printf("s1 = %d\n", s1);
printf("s2 = %d\n", s2);
printf("m = %d\n", m);
printf("d = %d\n", d);
return 0;
}
输出:s1 = 3
s2 = 5
m = 2
d = 4
预处理器在处理宏定义时,扮演一个“传话筒”的作用。宏表达式被预处理器处理,编译器不知道宏表达式的存在;
宏表达式用“实参”完全替代形参,不进行任何运算;
宏表达式没有任何的“调用”开销;
宏表达式不能出现递归定义。
#define _SUM_(n) ((n>0)?(_SUM_(n-1)+n):0)
int s = _SUM_(10);
宏定义的常量或表达式是否有作用域的限制呢?
作用域的概念,针对变量和函数,不针对宏定义。为什么呢?
因为宏定义是在预编译阶段被处理的,编译器根本不知道宏定义的存在。
强大的内置宏:
宏 含义 示例
__FILE__ 被编译的文件名 file.c
__LINE__ 当前行号 25
__DATE___ 编译时的日期 Jan 31 2012
__TIME__ 编译时的时间 17:01:02
__STDC__ 编译时时候遵循标准C规范 1
注意是双下划线。#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;
}
运行结果:
~/will$ ./a.out
[May 1 2018] {21-4.c:19} Begin to run main code...
0
1
2
3
4
[May 1 2018] {21-4.c:33} End
小结:预处理器直接对宏进行文本替换;
宏使用时的参数不会进行求值和运算;
预处理器不会对宏定义进行语法检查;
宏定义时出现的语法错误只能被编译器检测;
宏定义的效率高于函数调用;
宏的使用会带来一定的副作用。