0.对于宏的第一印象:
可以定义一个常量,如:#define PI 3.14159
可以定义一个代码块,将宏作为函数使用,如:
include <stdio.h>
// 此处将交换两个变量的函数用宏来定义
#define SWAP(a, b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int a = 1;
int b = 2;
printf("a = %d\n", a);
printf("b = %d\n", b);
SWAP(a,b);
printf("a = %d\n", a);
printf("b = %d\n", b);
return 0;
}
- 思考问题:
宏定义的常量和const定义的常量有什么区别? const 定义的常量本质还是变量,需要占用内存;宏常量的本质是常量,不需要占用内存。
宏代码块和真正的函数有什么区别?
2.C语言中的宏定义
#define是预处理器处理的单元实体之—
#define定义的宏可以出现在程序的任意位置
#define定义之后的代码都可以使用这个宏
3.定义宏常量
#define定义的宏常量可以直接使用
#define定义的宏常量本质为字面量
下面的宏常量定义正确吗?
#define ERROR -1
#define PATHl “D:\test\test.c”
#define PATH2 D:\test\test.c
#define PATH3 D:\test\
test.c
4.编程实验
宏定义分析
test.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* path1 = PATH1;
char* path2 = PATH2;
char* path3 = PATH3;
return 0;
}
由于宏被预处理器所处理,因此需要通过单步编译,查看预处理器后的.i文件。
单步编译代码如下:gcc -E test.c -o test.i
test.i
1 “test.c”
1 “”
1 “”
1 “/usr/include/stdc-predef.h” 1 3 4
1 “” 2
1 “test.c”
int main()
{
int err = -1;
char* path1 = “D:\test\test.c”;
char* path2 = D:\test\test.c;
char* path3 = D:\testtest.c;
return 0;
}
预处理器不会进行语法检查只是简单的文本替换,即这些宏定义都正确
宏定义正确,不代表编译就正确。预处理器直接进行文本替换,不做语法检查,它只将宏做了文本替换,没有进行语法检查。当预处理完之后,编译过程编译器才对语法做检查。
5.宏定义表达式
#define表达式的使用类似函数调用
#define表达式可以比函数更强大
#define表达式比函数更容易出错
判断下面的红表达式定义正确吗?
#define SUM(a, b) (a) + (b)
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define DIM(a) sizeof(a)/sizeof(*a)
宏表达式分析
#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;
}
查看预处理后的结果,通过单步编译得到test.i文件:
1 “test.c”
1 “”
1 “”
1 “/usr/include/stdc-predef.h” 1 3 4
1 “” 2
1 “test.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©/sizeof(*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的结果和我们预期的结果不一致。
总结:宏被预处理器所处理,预处理器直接进行文本替换,不做语法检查,得到的中间结果可能会在语义上发生变换,如上述实例中的s2。这就类似与我们的预编译器是一个传话筒,它将我们编写的源代码传递给真正进行语法和语义分析的编译器,而在传递过程中有可能产生歧义。这就是宏的副作用。
6.宏表达式与函数的对比
宏表达式被预处理器处理,编表译器不知道宏达式的存在
宏表达式用“实参“完全替代形参,不进行任何运算
宏表达式没有任何的“调用”开销
宏表达式中不能出现递归定义
#define SUM(n) ((n > 0) ? (SUM(n-1) + n): 0) //error
int s = SUM(10);
7.有趣的问题
宏定义的常量或表达式是否有作用域限制?
下面的程序合法吗?
void def()
{
#define PI 3.1415926
#define AREA® rrPI
}
double area(double r)
{
return AREA®;
}
宏的作用域分析
#include <stdio.h>
#include <stdlib.h>
void def()
{
#define PI 3.1415926
#define AREA® r * r * PI
}
double area(double r)
{
return AREA®;
}
int main()
{
double r = area(5);
printf("PI = %f\n", PI);
printf("d = 5; a = %f\n", r);
system("pause");
return 0;
}
编译结果如下:
预处理没有报错,这说明对于宏而言是没有作用域限制的。
作用域的概念是针对C语言里面的变量和函数的,它不针对宏,因为宏是被预处理器处理的。编译器不知道宏的存在。
8.强大的内置宏
宏使用综合示例
#include <stdio.h>
#include <malloc.h>
#define MALLOC(type, x) (type*)malloc(sizeof(type)*x)
#define FREE§ (free§, 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 “test.c”
1 “”
1 “<命令行>”
1 “test.c”
14 “test.c”
int main()
{
int x = 0;
int* p = (int*)malloc(sizeof(int)*5);
printf("[%s] {%s:%d} %s \n", “Aug 8 2019”, “test.c”, 19, “Begin to run main code…”); // 预处理后日期、文件名和行号为当前的时间、当前的文件名和当前的行号。
for(x=0; x<5; x++)
{
p[x] = x;
}
for(x=0; x<5; x++)
{
printf("%d\n", p[x]);
}
(free§, p=NULL);
printf("[%s] {%s:%d} %s \n", “Aug 8 2019”, “test.c”, 33, “End”);
return 0;
}
运行结果:
宏的强大之处,这些宏都是无法用函数替代的
9.小结
预处理器直接对宏进行文本替换
宏使用时的参数不会进行求值和运算
预处理器不会对宏定义进行语法检查
宏定义是出现的语法错误只能被编译器检测
宏定义的效率高于函数调用
宏的使用会带来一定的副作用
————————————————
版权声明:本文为CSDN博主「小虾米_2018」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_22847457/article/details/98874000@TOC