C语言预处理学习记录

#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
",a,b);
	printf("max_wrong(2,max_wrong(3,4)) = %d
",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
",max(2,max(3,4)));
	
	int a=1;
	printf("%d
",max(a++,0));
	log("%d
",a);
	a=1;
	log("%d
",maxwell(a++,0));
	log("%d
",a);
	
	log("Hello world");
	return 0;
}
原文地址:https://www.cnblogs.com/chwhc/p/12373580.html