This question already has answers here:
Closed 2 years ago.
#define macro for debug printing in C?
(12个答案)
据我所知,assert是C语言中的一个宏,如果您在编译时使用它,但不启用它,那么就不会有开销(这可能不正确,我不知道)。
对我来说,问题是我想做的是获取传递给我的函数的所有变量并打印出输出,但前提是我想启用调试以下是我目前掌握的情况:
int exampleFunction (int a, int b)
{
  #ifdef debugmode
  printf("a = %i, b = %i", a, b);
  #endif
}

我想知道有没有什么更简单(也不那么难看)的方法来做这样的事情xdebug for php有这个特性,我发现它在调试时为我节省了大量的时间,所以我想为每个函数都这样做。
谢谢

最佳答案

试试这个:

#ifdef debugmode
#define DEBUG(cmd) cmd
#else
#define DEBUG(cmd)
#endif


DEBUG(printf("a = %i, b = %i", a, b));

现在,如果定义了debugmode,就可以得到打印语句否则,它永远不会出现在二进制文件中。

07-28 06:05