学习自狄泰软件学院唐佐林老师C语言课程,文章中图片取自老师的PPT,仅用于个人笔记。
实验1 : 宏是被预处理器所处理的,预处理器只进行简单的文本替换,不会进行语法检测
实验2 : 宏表达式的强大和易出错
实验3:宏的作用域,对于宏而言 是没有作用域限制的,作用域的概念是针对C语言里面的变量和函数的,不针对宏。因为宏是被预处理器处理的,编译器根本不知道宏标识符的存在,所以编译器根本无法将作用域的概念应用于宏!
实验4: 强大的内置宏
‘
define 定义出来的宏常量本质是字面量,也就是说 不占用任何的内存空间。
实验1 : 宏是被预处理器所处理的,预处理器只进行简单的文本替换,不会进行语法检测
#define ERROR -1
#define PATH1 "D:\test\test.c"
#define PATH2 D:\test\test.c //error
#define PATH3 D:\test\ //error // 宏中的接续符
test.c
int main()
{
int err = ERROR;
char* p1 = PATH1;
char* p2 = PATH2;
char* p3 = PATH3;
}
宏是被预处理器所处理的,因此我们判断这几个宏定义的对不对就需要看预处理器出来的结果是否正确,
gcc -E 21-1.c -o 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;
}
mhr@ubuntu:~/work/C$
mhr@ubuntu:~/work/C$
mhr@ubuntu:~/work/C$ gcc -E 21-1.c -o 21-1.i
mhr@ubuntu:~/work/C$ ll
total 28
drwxrwxr-x 2 mhr mhr 4096 Jan 11 19:23 ./
drwxrwxr-x 4 mhr mhr 4096 Dec 15 03:18 ../
-rw------- 1 mhr mhr 225 Nov 17 2014 21-1.c
-rw-rw-r-- 1 mhr mhr 260 Jan 11 19:23 21-1.i
-rw------- 1 mhr mhr 485 Nov 17 2014 21-2.c
-rw------- 1 mhr mhr 292 Nov 17 2014 21-3.c
-rw------- 1 mhr mhr 606 Nov 17 2014 21-4.c
mhr@ubuntu:~/work/C$
mhr@ubuntu:~/work/C$ gcc 21-1.c
21-1.c: In function ‘main’:
21-1.c:4:15: error: ‘D’ undeclared (first use in this function)
#define PATH2 D:\test\test.c
^
21-1.c:12:16: note: in expansion of macro ‘PATH2’
char* p2 = PATH2;
^
21-1.c:4:15: note: each undeclared identifier is reported only once for each function it appears in
#define PATH2 D:\test\test.c
^
21-1.c:12:16: note: in expansion of macro ‘PATH2’
char* p2 = PATH2;
^
21-1.c:4:16: error: expected ‘,’ or ‘;’ before ‘:’ token
#define PATH2 D:\test\test.c
^
21-1.c:12:16: note: in expansion of macro ‘PATH2’
char* p2 = PATH2;
^
21-1.c:4:16: error: stray ‘\’ in program
#define PATH2 D:\test\test.c
^
21-1.c:12:16: note: in expansion of macro ‘PATH2’
char* p2 = PATH2;
^
21-1.c:4:16: error: stray ‘\’ in program
#define PATH2 D:\test\test.c
^
21-1.c:12:16: note: in expansion of macro ‘PATH2’
char* p2 = PATH2;
^
21-1.c:5:16: error: expected ‘,’ or ‘;’ before ‘:’ token
#define PATH3 D:\test\
^
21-1.c:13:16: note: in expansion of macro ‘PATH3’
char* p3 = PATH3;
^
21-1.c:5:16: error: stray ‘\’ in program
#define PATH3 D:\test\
^
21-1.c:13:16: note: in expansion of macro ‘PATH3’
char* p3 = PATH3;
^
mhr@ubuntu:~/work/C$
实验2 : 宏表达式的强大和易出错
// #include <stdio.h>
#define _SUM_(a, b) (a) + (b)
#define _MIN_(a, b) ((a) < (b) ? (a) : (b))
// 求 数组的大小,C语言中无法通过函数实现求得一个数组的大小,但是宏表达式可以 强大!
#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;
}
单步编译:gcc -E 21-2.c -o 21-2.i
# 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);
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;
}
mhr@ubuntu:~/work/C$ gcc -E 21-2.c -o 21-2.i
mhr@ubuntu:~/work/C$
mhr@ubuntu:~/work/C$ gcc 21-2.c
21-2.c: In function ‘main’:
21-2.c:19:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
printf("s1 = %d\n", s1);
^
21-2.c:19:5: warning: incompatible implicit declaration of built-in function ‘printf’
21-2.c:19:5: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
mhr@ubuntu:~/work/C$
mhr@ubuntu:~/work/C$ ./a.out
s1 = 3
s2 = 5
m = 2
d = 4
mhr@ubuntu:~/work/C$
结果的分析要看 预处理后的文件才能看清!!!!
实验3:宏的作用域,对于宏而言 是没有作用域限制的,作用域的概念是针对C语言里面的变量和函数的,不针对宏。因为宏是被预处理器处理的,编译器根本不知道宏标识符的存在,所以编译器根本无法将作用域的概念应用于宏!
// #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;
}
gcc -E 21-3.c -o 21-3.i
# 1 "21-3.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "21-3.c"
void def()
{
}
double area(double r)
{
return r * r * 3.1415926;
}
int main()
{
double r = area(5);
printf("PI = %f\n", 3.1415926);
printf("d = 5; a = %f\n", r);
return 0;
}
.
mhr@ubuntu:~/work/C$ gcc 21-3.c
21-3.c: In function ‘main’:
21-3.c:18:6: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
printf("PI = %f\n", PI);
^
21-3.c:18:6: warning: incompatible implicit declaration of built-in function ‘printf’
21-3.c:18:6: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
mhr@ubuntu:~/work/C$
mhr@ubuntu:~/work/C$ ./a.out
PI = 3.141593
d = 5; a = 78.539815
mhr@ubuntu:~/work/C$
实验4: 强大的内置宏
#include <stdio.h>
#include <malloc.h>
//申请数组空间,C语言中的函数 是不能将 类型 作为参数调用的 这是 宏的强大的体现
#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) //打印log信息
#define FOREACH(i, m) for(i=0; i<m; i++) // 宏比函数强大的例子
#define BEGIN {
#define END }
int main()
{
int x = 0;
//C语言中的函数 是不能将 类型 作为参数调用的 这是 宏的强大的体现
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;
}