I am confused about the if statement in macro. Here is my code:
#ifdef SHOW_LOGS_
#define LOG(operation, parameter, value) \
do { \
if ((parameter) == (NULL)){ \
fprintf(stderr,"%s:%d: %s\n",__FILE__,__LINE__,operation);\
}else { \
fprintf(stderr,"%s:%d: %s, [%s] : %d\n",__FILE__,__LINE__,operation,parameter,value);\
} \
} while(0)
#else
#define LOG
#endif
But, when I test my code like:
LOG("ROUNDING UP...",NULL,NULL);
The compiler gave me a warning, which tells me that the test code above turn to execute
fprintf(stderr,"%s:%d: %s\n",__FILE__,__LINE__,operation);
not the right one. So the if statement didn't work.
And here is another example:
#define TEST_EQ_BASE(equality,expect,actual,format) \
do { \
test_count++; \
if (equality){ \
test_pass++;\
}else{ \
main_ret = 1; \
fprintf(stderr,"%s:%d: expect: " format " actual: " format "\n",__FILE__,__LINE__,expect,actual);\
} \
}while (0)
In this code, the if statement works.
I am very confused about it, why is the difference?
Thank you!
Edit:
Sorry you guys, I am a junior student, not so familiar with the macro, so sorry.
I checked the result, the if statement did worked, but the compiler still complains:
uninitialized.h:88:2: note: in expansion of macro ‘LOG’
LOG("__uninitialized_copy_aux: copying without constructor...",NULL,NULL);
^
stl_config.h:53:87: warning: format ‘%d’ expects argument of type ‘int’, but argument 7 has type ‘long int’ [-Wformat=]
fprintf(stderr,"%s:%d: %s, [%s] : %d\n",__FILE__,__LINE__,operation,parameter,value);\
This is the output:
uninitialized.h:88: __uninitialized_copy_aux: copying without constructor...
This is the macro expanded:
do { if ((__null) == (__null))
{ fprintf(stderr,"%s:%d: %s, [%s] : %d\n","uninitialized.h",80,"__uninitialized_copy_aux: copying with constructor...",__null,__null); }else
{ fprintf(stderr,"%s:%d: %s\n","uninitialized.h",80,"__uninitialized_copy_aux: copying with constructor..."); } } while(0);
So I think the problem is when passing NULL to the macro, the compiler gives the warning, can I eliminate the warning?
Thank you!!