在C中调试打印宏? [英] Debug Print Macro in C?

查看:81
本文介绍了在C中调试打印宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C语言中,定义仅在定义DEBUG符号时才打印的类似于printf的宏的正确方法是什么?

in C, what is the proper way to define a printf like macro that will print only when DEBUG symbol is defined?

#ifdef DEBUG
#define DEBUG_PRINT(???) ???
#else
#define DEBUG_PRINT(???) ???
#endif

其中???是我不确定要填写的地方

where ??? is where I am not sure what to fill in

推荐答案

我已经看到了很多这样的成语:

I've seen this idiom a fair amount:

#ifdef DEBUG
# define DEBUG_PRINT(x) printf x
#else
# define DEBUG_PRINT(x) do {} while (0)
#endif

使用方式如下:

DEBUG_PRINT(("var1: %d; var2: %d; str: %s\n", var1, var2, str));

多余的括号是必需的,因为某些较旧的C编译器不支持宏中的var-args.

The extra parentheses are necessary, because some older C compilers don't support var-args in macros.

这篇关于在C中调试打印宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆