宏在预编译阶段就被替换了,下面例子展示了:宏参数传递、宏连接、宏换行。
#define INITSTOP_ERR 10001 //error number
#define INITSTOP_LVL 1 //error level
#define INITSTOP_DSC "zxinit process stoped. total error: %d\n" //error description
#define WZXLOG2(ERRNM) printf("%d; %d.",ERRNM##_ERR, ERRNM##_LVL);\
printf(INITSTOP_DSC
使用宏打印错误信息只需要这样:
WZXLOG2(INITSTOP), 33); //打印INITSTOP类型错误
#define INITSTOP_ERR 10001 //error number
#define INITSTOP_LVL 1 //error level
#define INITSTOP_DSC "zxinit process stoped. total error: %d\n" //error description
#define WZXLOG2(ERRNM) printf("%d; %d.",ERRNM##_ERR, ERRNM##_LVL);\
printf(INITSTOP_DSC
使用宏打印错误信息只需要这样:
WZXLOG2(INITSTOP), 33); //打印INITSTOP类型错误
本文介绍了宏定义在预编译阶段的应用,包括宏参数传递、宏连接与宏换行等特性。通过示例展示了如何利用宏定义简化错误日志的输出过程。
1140

被折叠的 条评论
为什么被折叠?



