I write an example to show my problem, here it is to use nested macros to make calls to check the number is positive and odd. I know this is redundant to use this call, but as I said previously, it just show that if using macro in nested way, there is some problem:
#include <stdio.h>
#include <stdlib.h>
#define num_is_positive_odd(num) \
({ \
int __rc = 0; \
int __num = (num); \
\
printf("%s:%d: check linenum\n", __func__, __LINE__); \
if (num_is_positive(__num) && num_is_odd(__num)) \
__rc = 1; \
__rc; \
})
#define num_is_positive(num) \
({ \
int __rc = 0; \
int __num = (num); \
\
if (__num > 0) { \
printf("%s: number %d is positive\n", \
__func__, __num); \
__rc = 1; \
} \
__rc; \
})
#define num_is_odd(num) \
({ \
int __rc = 0; \
int __num = (num); \
\
if (__num / 2) { \
printf("%s: number %d is odd\n", \
__func__, __num); \
__rc = 1; \
} \
__rc; \
})
int main()
{
int num = 4;
if (num_is_positive_odd(num++))
printf("%s: number %d is positive odd\n", __func__, num);
exit(0);
}
When compile it using command: gcc -Wunused-variable chk_comps.c
it shows error:
chk_comps.c: In function ‘main’:
chk_comps.c:7:9: warning: unused variable ‘__num’ [-Wunused-variable]
int __num = (num); \
^
chk_comps.c:47:9: note: in expansion of macro ‘num_is_positive_odd’
if (num_is_positive_odd(num++))
^
`
Could somebody help explain why and how to fix it? thanks.