#用C定义调试打印的宏?尝试创建一个宏,在定义调试时可用于打印调试消息,如以下伪代码:#define DEBUG 1#define debug_print(args ...) if (DEBUG) fprintf(stderr, args)这是如何通过宏实现的?
3 回答
慕桂英546537
TA贡献1848条经验 获得超10个赞
#ifdef DEBUG #define D if(1) #else #define D if(0) #endif
D printf("x=%0.3f\n",x);
printf
else
if
#ifdef DEBUG #define D #else #define D for(;0;)#endif
一只名叫tom的猫
TA贡献1906条经验 获得超3个赞
#include <stdio.h>#include <stdarg.h>#ifndef NDEBUG# define debug_print(msg) stderr_printf msg#else# define debug_print(msg) (void)0#endifvoidstderr_printf(const char *fmt, ...){ va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap);}intmain(int argc, char *argv[]){ debug_print(("argv[0] is %s, argc is %d\n", argv[0], argc)); return 0;}
#include <stdio.h>#define _ ,#ifndef NDEBUG# define debug_print(msg) fprintf(stderr, msg)#else# define debug_print(msg) (void)0#endifintmain(int argc, char *argv[]){ debug_print("argv[0] is %s, argc is %d"_ argv[0] _ argc); return 0;}
- 3 回答
- 0 关注
- 666 浏览
添加回答
举报
0/150
提交
取消