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

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

问题描述

在 C 中,定义一个类似 printf 的宏的正确方法是什么,该宏仅在定义了 DEBUG 符号时才打印?

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

推荐答案

这个成语我见多了:

#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
", 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天全站免登陆