I have the following macro to print out the contents of an array of integers for debug purposes:
#define PRINT_ARRAY(ary, num) \
int ai = 0; \
printf("{"); \
for(ai=0; ai < num; ++ai) { \
printf("%d", ary[ai]); \
if(ai < num-1) printf(", "); \
} \
printf("}\n");
The problem I am having is that when I use it more than once, some compilers complain that I am redefining ai
.
Is there a way to make the identifier different for each invocation of the macro? I can come up with a naming scheme that would have a very low change of name collisions, but I'd like to make it automatic.
I know I could use a function, but I am still curious, since I would like to know if there is a way to do this with macros.