2014-11-24 72 views
2
#include<stdio.h> 
#include<errno.h> 
#include<error.h> 
#include<stdlib.h> 

#define clean_errno() (errno == 0 ? "None" : strerror(errno)) 
#define DEBUG_ERROR(M,...) fprintf(stderr, "[ERROR] (%s:%d: (error: %s)) M \n", __FILE__, __LINE__, clean_errno(),##__VA_ARGS__) 

int main() 
{ 
    int test =10; 
    DEBUG_ERROR("Test variable %s = %d.\n","test",test); 
    return 0; 
} 

關於如何解決與調試宏以下警告任何想法太多的爭論......警告:對格式

warn.c:12:4:警告:參數太多的格式[ - Wformat-額外參數]

+0

請注意,您的宏使用''##'構造的gcc擴展。 – 2014-11-24 16:44:59

回答

3

您的宏應該是:

#define DEBUG_ERROR(M,...) fprintf(stderr, "[ERROR] (%s:%d: (error: %s)) " M " \n", __FILE__, __LINE__, clean_errno(),##__VA_ARGS__) 

注意M不報價了。