c++宏调试

使用工程调试

如果是linux环境,注意这里的fmt前面要有一个空格
否则会报错
error: unable to find string literal operator ‘operator""fmt’ with ‘const char [4]’, ‘long unsigned int’ arguments

代码

#include <stdio.h>
#include <stdlib.h>

#define DEBUG_ON 
#ifdef  DEBUG_ON
	#define  DEBUG(fmt, args...)  	printf("[FILE = %s][FUNCTION = %s][LINE = %d] ["fmt"]\n",__FILE__,__FUNCTION__,__LINE__,##args)
#else
	#define  DEBUG(fmt, args...)  	do{
      
      }while(0)
#endif

int main()
{
    
    
	int a = 0;
	DEBUG("a = %d",a);
	DEBUG("test");
	return 0;
}

结果:

 [FILE = debug.c][FUNCTION = main][LINE = 15] [a = 0] 
 [FILE = debug.c][FUNCTION = main][LINE = 16] [test]

算法题目调试

注意这里的fmt前面要有一个空格
因为大部分oj运行环境是linux,在linux上面前面需要有一个空格。

#define DEBUG_ON 
#ifdef  DEBUG_ON
	#define  DEBUG(fmt, args...)  	printf("" fmt"",##args)
#else
	#define  DEBUG(fmt, args...)  	do{
      
      }while(0)
#endif

int main() {
    
    
	int n = 5;
    int b[] = {
    
    1, 2, 2, 2, 2};
    DEBUG("b[] = ");
    for (int i = 0; i < n; i++) {
    
    
        DEBUG("%lld ", b[i]);
    }
    DEBUG("\n");
    return 0;
}

结果

b[] = 1 2 2 2 2 

猜你喜欢

转载自blog.csdn.net/fuzekun/article/details/129198268