您可以使用
GNU C Language Extensions:
#define DEBUG(x) \
({ \
if (__builtin_types_compatible_p (typeof (x), int)) \
fprintf(stderr,"%d\n",x); \
else if (__builtin_types_compatible_p (typeof (x), char)) \
fprintf(stderr,"%c\n",x); \
else if (__builtin_types_compatible_p (typeof (x), char[])) \
fprintf(stderr,"%s\n",x); \
else \
fprintf(stderr,"unknown type\n"); \
})
这些都很好:
DEBUG("hello"); //prints hello
DEBUG(11110); //prints 11110
但是对于chars,你应该使用它与lvalue,否则它的类型将是“int”:
char c='A';
DEBUG(c); // prints A
DEBUG('A'); // prints 65