ç调试打印宏 [英] C Debug Print Macros

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

问题描述

在C,什么是定义类似宏一个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编译器不支持无功ARGS中的宏。

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

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

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