宏参数
# 字符串化操作符
#define example(instr) #instr
注:
1.忽略传入参数名前面和后面的空格
2.当传入参数名间存在空格时,编译器将会自动连接各个子字符串,用每个子字符串中只以一个空格连接,忽略其中多余一个的空格
{
char *buf = example(abc);
puts(buf);
}
abc
将宏定义的多个形参成一个实际参数名
int num=example(9); 将会扩展成 int num=num9;
注:
1.当用
2.连接后的实际参数名,必须为实际存在的参数名或是编译器已知的宏定义
{
int num9 = 3;
int data = example(9);
printf("%d\n", data);
}
3
@# 字符化操作符
#define makechar(x) #@x
a = makechar(b); 展开后变成了:a= 'b';
测试未通过
宏定义输出
#define LOG(format, ...) fprintf(stdout, format, __VA_ARGS__)
#define LOG(format, args...) fprintf(stdout, format, args)
#define LOG(format, ...) fprintf(stdout, format, ##__VA_ARGS__)
#define LOG(format, args...) fprintf(stdout, format, ##args)
#ifdef DEBUG
#define LOG(format, args...) \
fprintf(stdout, "%s:%d: %s: %m: "format"\n", __FILE__, __LINE__, __FUNCTION__, ##args);
#else
#define LOG(format, args...)
#endif
宏定义中do{ }while(0)
1. 空的宏定义避免warning
#define foo() do{}while(0)
2. 存在一个独立的block,可以用来进行变量定义,进行比较复杂的实现
3. 如果出现在判断语句过后的宏,这样可以保证作为一个整体来是实现
if(NULL == pPointer)
foo();
ANSI C中标准预定义宏
__LINE__:在源代码中插入当前源代码行号;
__FILE__:在源文件中插入当前源文件名;
__DATE__:在源文件中插入当前的编译日期
__TIME__:在源文件中插入当前编译时间;
__STDC__:当要求程序严格遵循ANSI C标准时该标识被赋值为1;
__cplusplus:当编写C++程序时该标识符被定义
__FUNCTION__:函数名
打印日志到文件
{ \
time_t timeval; \
timeval=time(NULL); \
FILE *log; \
log = fopen("log.txt","a");\
fprintf(log,"%s -- %s\n",p,ctime(&timeval));\
fclose(log); \
}