0%

C语言预处理学习记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<stdio.h>

#define LOCAL //无参宏

//条件编译
#ifdef LOCAL
int a=1;
#else
int a=2;
#endif

#ifndef LOCAL
int b=1;
#else
int b=2;
#endif

#define PI 3.1415926535 //有参宏

#define max_wrong(a,b) a>b?a:b
#define max(a,b) ((a)>(b)?(a):(b))
#define maxwell(a,b) ({\
__typeof(a) _a=(a),_b=(b);\
_a>_b?_a:_b;\
})

#define DEBUG//

#ifdef DEBUG
#define log(frm,args...) {\
printf("[%s %s %d] : %s = ",__FILE__,__func__,__LINE__,#args);\
printf(frm,##args);\
}
#else
#define log(frm,args...)
#endif

int main(){
printf("a = %d b = %d\n",a,b);
printf("max_wrong(2,max_wrong(3,4)) = %d\n",max_wrong(2,max_wrong(3,4)));
//展开后: 2>3>4?3:4?2:3>4?3:4 实际执行 ((2>3>4)?(3):(4))? (2) : (3>4?3:4)
//使用 -E 编译选项获得预编译结果
printf("max(2,max(3,4)) = %d\n",max(2,max(3,4)));

int a=1;
printf("%d\n",max(a++,0));
log("%d\n",a);
a=1;
log("%d\n",maxwell(a++,0));
log("%d\n",a);

log("Hello world");
return 0;
}