I have a trace() macro I turn on and off with another macro, e.g.
#ifdef TRACE
#define trace(x) trace_val(x, 0)
#else
#define trace(x) 0
#endif
This generates warning: statement with no effect from gcc when I call trace() with TRACE undefined. After a little searching I found that changing
#define trace(x) 0
to
#define trace(x) (void)0
silences the error. My question is: Why? What’s the difference?
The cast to void makes it clear that the programmer intends to throw the result away. The purpose of the warning is to indicate at that it’s not obvious that the statement has no effect and thus it’s useful to alert the programmer to that in case it was unintentional. A warning here would serve no purpose.