The problem with your current approach is that you are introducing lots of if-statements where you don't need them. Here's a better approach, based on the same principle:
Code:
#ifdef DEBUG
#define DMSG(msg) \
{ printf("%s:%d: ", __FILE__, __LINE__); \
printf msg; \
puts(""); }
#else
#define DMSG(msg)
#endif
This introduces the macro DMSG which will print filename, line number and your debug message if DEBUG is defined, and will expand to the empty statement otherwise, thus not wasting ressources.
Note that you will need double parantheses in order for this to work properly:
Code:
DMSG(("foo is %d", foo));
will expand to "<source file>:<line number>: foo is <foo>\n".
If you're using C99, you can also use:
Code:
#define DEBUG(...) \
{ printf("%s:%d:%s(): ", __FILE__, __LINE__, __func__); \
printf(__VA_ARGS__); \
puts(""); }
This will also print the corresponding function name, and you don't need double parenthesis.
Greets,
Philip